Archive: Programing PIC16f873 for PWM

Status
Not open for further replies.

Thread Starter

damochi

Joined Feb 17, 2006
40
hi please i need help in getting the programming code for Pulse width modulation for PIC16F873 so that when i feed in a variable analogue signal between 0V and 2.047V it shows a full deflection for 2.047V on a moving coil and it goes back to ZERO for 0V please help me it's a school project and i'm really new to PIC Programming
 

Papabravo

Joined Feb 24, 2006
21,158
Originally posted by damochi@Mar 1 2006, 07:26 PM
hi please i need help in getting the programming code for Pulse width modulation for PIC16F873 so that when i feed in a variable analogue signal between 0V and 2.047V it shows a full deflection for 2.047V on a moving coil and it goes back to ZERO for 0V please help me it's a school project and i'm really new to PIC Programming
[post=14516]Quoted post[/post]​
Do you want to sample an analog value with an A/D converter, and use that value to output a PWM signal?

What exactly do you mean by a "moving coil"?

Do you want the PWM signal to be on all the time if the input is at "full scale" and off all the time when the input is at zero?
 

Thread Starter

damochi

Joined Feb 17, 2006
40
Originally posted by Papabravo@Mar 1 2006, 11:34 PM
Do you want to sample an analog value with an A/D converter, and use that value to output a PWM signal?

What exactly do you mean by a "moving coil"?

Do you want the PWM signal to be on all the time if the input is at "full scale" and off all the time when the input is at zero?
[post=14518]Quoted post[/post]​
Basically yu knida get the gist but i need to convert analogue signal to digital and back to analogue yu get it ? i know it's pointless but it's an assesment the moving coil is meant to be the output display so please help
 

Papabravo

Joined Feb 24, 2006
21,158
Originally posted by damochi@Mar 1 2006, 09:40 PM
Basically yu knida get the gist but i need to convert analogue signal to digital and back to analogue yu get it ? i know it's pointless but it's an assesment the moving coil is meant to be the output display so please help
[post=14521]Quoted post[/post]​
OK, I think I understand. The "moving coil" is an old analog voltmeter. So the problem is to do an A/D conversion and use the resulting number to create a PWM output signal which can be run thru a low pass filter to average(integrate) the PWM signal to convert it back to an analog value. Is this correct?

While waiting for you to answer I will consult the PIC16F873 data sheet for some programming tips on generating a PWM signal.
 

Thread Starter

damochi

Joined Feb 17, 2006
40
Originally posted by Papabravo@Mar 2 2006, 02:26 AM
OK, I think I understand. The "moving coil" is an old analog voltmeter. So the problem is to do an A/D conversion and use the resulting number to create a PWM output signal which can be run thru a low pass filter to average(integrate) the PWM signal to convert it back to an analog value. Is this correct?

While waiting for you to answer I will consult the PIC16F873 data sheet for some programming tips on generating a PWM signal.
[post=14522]Quoted post[/post]​
Yes yu are correct thanks i'll be expecting an answer
 

Papabravo

Joined Feb 24, 2006
21,158
Originally posted by damochi@Mar 2 2006, 07:57 PM
Yes yu are correct thanks i'll be expecting an answer
[post=14551]Quoted post[/post]​
The A/D converter on the PIC16F873 produces a 10 bit value. You can choose to map the 10 bits into register in two different ways. For your purposes the the left-justified result would be preferred (p.116 of the PIC16F873 datasheet). I'm assuming here that you can program the A/D Converter to get an analog value.

On to the PWM
Look at page 62 of the PIC16F873 datasheet.
Table 8-3 shows how to pick the frequency for a 20MHz clock.
For your purposes I would select the 1.22kHz frequency. The "moving coil analog voltmeter" is unlikely to be able to follow this frequency. Of course you should feel free to experiment with other values.

Anyway the PWM setup instructions are just above Table 8-3

Good Luck
 

Thread Starter

damochi

Joined Feb 17, 2006
40
Originally posted by Papabravo@Mar 3 2006, 05:48 PM
The A/D converter on the PIC16F873 produces a 10 bit value. You can choose to map the 10 bits into register in two different ways. For your purposes the the left-justified result would be preferred (p.116 of the PIC16F873 datasheet). I'm assuming here that you can program the A/D Converter to get an analog value.

On to the PWM
Look at page 62 of the PIC16F873 datasheet.
Table 8-3 shows how to pick the frequency for a 20MHz clock.
For your purposes I would select the 1.22kHz frequency. The "moving coil analog voltmeter" is unlikely to be able to follow this frequency. Of course you should feel free to experiment with other values.

Anyway the PWM setup instructions are just above Table 8-3

Good Luck
[post=14578]Quoted post[/post]​
i still don't get this cos i'm a real novice to PIC Programming and u can even generate an algorithm
 

Papabravo

Joined Feb 24, 2006
21,158
Originally posted by damochi@Mar 3 2006, 06:35 PM
i still don't get this cos i'm a real novice to PIC Programming and u can even generate an algorithm
[post=14595]Quoted post[/post]​
Even a novice has to start somewhere. Read the data sheet chapters which describe the peripherals of interest. This would be the A/D converter and the Compare Capture Module. Each of these peripherals has one or more registers which control the operation of that device.

In order to get a value into a register in the PIC architecture you need to understand the operation of the following two instructions

1. MOVLW k ; move literal(constant) to register W
2. MOVWF f ; move register W to file register f

For example if we want to put the value 0x27 into the Timer 2 Period Register PR2 we might write in assembly language

MOVLW 0x27
MOVWF PR2

If you will be programming with a C-compiler it might look like

PR2 = 0x27 ;

In any case, even if I wrote the code for you, understanding and modifying the code would be difficult and you will have learned nothing. Please be patient, go to the microchip website, get the data sheet, try to do this on your own. I was similarly confounded when I wrote my first progam in the fall of 1962. Trust me the feeling of accomplishment will greatly outweigh the investment of effort.
 

Thread Starter

damochi

Joined Feb 17, 2006
40
please what i don't understand is what value i'm i writing to the register is it the chosen frequency yu caculated in the other reply or the what and i guessing i need to change it to hexadecimal as well
 

Papabravo

Joined Feb 24, 2006
21,158
Originally posted by damochi@Mar 5 2006, 02:34 PM
please what i don't understand is what value i'm i writing to the register is it the chosen frequency yu caculated in the other reply or the what and i guessing i need to change it to hexadecimal as well
[post=14690]Quoted post[/post]​
There is a table in the PIC 16F873 datasheet on page 62. It is labeled Table 8-3
Let us examine this table in some detail. We start with the title of the table
"EXAMPLE PWM FREQUENCIES AND RESOLUTIONS AT 20MHZ"

What does the 20 Mhz. mean? It is the frequency of the PIC processor's crystal. If your PIC is not running at 20MHz., then we have to work harder, but can still use this table as a guide.

What is the PWM frequency? This is the reciprocal of the period, which is the time it takes for the PWM machine to make two state changes, either 0-1-0 or 1-0-1. I think you already know this.

In the column labeled 1.22kHz., there are three entries. The first entry is the Timer 2 prescaler value that you need to produce a PWM frequency of 1.22 KHz. Now your homework is to find the register and the combination of bits required to selct a prescaler value of 16 for the Timer 2 prescaler.

The second entry in Table 8-3 is the PR2 value. In the column labeled 1.22kHz. this value is 0xFFh. This is an 8-bit value of all ones. This constant is actually expressed using two different conventions for indicating the hexidecimal number base. The leading "0x..." is the C-language convention. The trailing ".....h" used in many assemblers indicates the same thing. So this tells us that in order to select the PWM frequency of 1.22 KHz we need to write an 0xFF into register PR2.

MOVLW 0xFF ; move 0xFF to register W
MOVWF PR2 ; move register W to PR2

The last entry in table 8-3 is for resolution. In the column for 1.22 kHz it says 10. It means that at this frequency of 1.22 Khz a one bit change in the duty cycle value will produce an actual change in the duty cycle. Just the thing we want from a 10-bit A/D Converter.

That's all for now. Read the datasheet and answer the homework question for the next installment.
 

Thread Starter

damochi

Joined Feb 17, 2006
40
Originally posted by Papabravo@Mar 5 2006, 07:11 PM
There is a table in the PIC 16F873 datasheet on page 62. It is labeled Table 8-3
Let us examine this table in some detail. We start with the title of the table
"EXAMPLE PWM FREQUENCIES AND RESOLUTIONS AT 20MHZ"

What does the 20 Mhz. mean? It is the frequency of the PIC processor's crystal. If your PIC is not running at 20MHz., then we have to work harder, but can still use this table as a guide.

What is the PWM frequency? This is the reciprocal of the period, which is the time it takes for the PWM machine to make two state changes, either 0-1-0 or 1-0-1. I think you already know this.

In the column labeled 1.22kHz., there are three entries. The first entry is the Timer 2 prescaler value that you need to produce a PWM frequency of 1.22 KHz. Now your homework is to find the register and the combination of bits required to selct a prescaler value of 16 for the Timer 2 prescaler.

The second entry in Table 8-3 is the PR2 value. In the column labeled 1.22kHz. this value is 0xFFh. This is an 8-bit value of all ones. This constant is actually expressed using two different conventions for indicating the hexidecimal number base. The leading "0x..." is the C-language convention. The trailing ".....h" used in many assemblers indicates the same thing. So this tells us that in order to select the PWM frequency of 1.22 KHz we need to write an 0xFF into register PR2.

MOVLW 0xFF ; move 0xFF to register W
MOVWF PR2 ; move register W to PR2

The last entry in table 8-3 is for resolution. In the column for 1.22 kHz it says 10. It means that at this frequency of 1.22 Khz a one bit change in the duty cycle value will produce an actual change in the duty cycle. Just the thing we want from a 10-bit A/D Converter.

That's all for now. Read the datasheet and answer the homework question for the next installment.
[post=14692]Quoted post[/post]​
so what yu saying is the value 1.22khz will produce the voltage range i want and which homework questions are they in the data sheet
 

Thread Starter

damochi

Joined Feb 17, 2006
40
Originally posted by damochi@Mar 6 2006, 01:41 PM
so what yu saying is the value 1.22khz will produce the voltage range i want and which homework questions are they in the data sheet
[post=14715]Quoted post[/post]​
and can yu please tell me the sections to read cos i honestly dunno
 

Mazaag

Joined Oct 23, 2004
255
Originally posted by Papabravo@Mar 3 2006, 07:06 PM
Even a novice has to start somewhere. Read the data sheet chapters which describe the peripherals of interest. This would be the A/D converter and the Compare Capture Module. Each of these peripherals has one or more registers which control the operation of that device.

In order to get a value into a register in the PIC architecture you need to understand the operation of the following two instructions

1. MOVLW k ; move literal(constant) to register W
2. MOVWF f ; move register W to file register f

For example if we want to put the value 0x27 into the Timer 2 Period Register PR2 we might write in assembly language

MOVLW 0x27
MOVWF PR2

If you will be programming with a C-compiler it might look like

PR2 = 0x27 ;

In any case, even if I wrote the code for you, understanding and modifying the code would be difficult and you will have learned nothing. Please be patient, go to the microchip website, get the data sheet, try to do this on your own. I was similarly confounded when I wrote my first progam in the fall of 1962. Trust me the feeling of accomplishment will greatly outweigh the investment of effort.
[post=14610]Quoted post[/post]​

Where do we define the address "value" of the PR2 register? or is this picked up automatically ? reason i'm asking , is because I thought that we have to specify the ADDRESS of the register in hex of where we writing to .
 

Papabravo

Joined Feb 24, 2006
21,158
Originally posted by Mazaag@Mar 6 2006, 01:44 PM
Where do we define the address "value" of the PR2 register? or is this picked up automatically ? reason i'm asking , is because I thought that we have to specify the ADDRESS of the register in hex of where we writing to .
[post=14720]Quoted post[/post]​
Mazaag
If you look at the fragment below of the include file for the PIC16F873 you will see definitions using EQU statements for registers defined in that particular variation of the PIC 16F family. One of the things an assembler does for you is to remove the necessity for remembering numerical addresses for a collection of register and variable names. A C compiler will do the same thing for you using #define statements.

The file says that PR2 is EQU H'0092' which in binary is "1001 0010"
Remember that register addresses in the PIC architecture are seven bits so you have to set the register select bits so that you are accessing register bank 1.

PCON EQU H'008E'
SSPCON2 EQU H'0091'
PR2 EQU H'0092'
SSPADD EQU H'0093'
SSPSTAT EQU H'0094'

hope this helps
 

Papabravo

Joined Feb 24, 2006
21,158
Originally posted by damochi@Mar 6 2006, 09:56 AM
and can yu please tell me the sections to read cos i honestly dunno
[post=14716]Quoted post[/post]​
damochi

No I am not saying that at all. The frequency of 1.22kHz. is a starting point for constructing a PWM signal. In the end you will do an A/D conversion that will produce a 10 bit result. You will write that 10 bit result to a pair of PWM registers. The resulting average of high and low voltages produced by the PWM will be averaged by the moving coil into a replica of the original analog voltage.

In my previous replies I have told you twice which sections of the datasheet to read. Once again you need to read:
1. Chapter 8 pages 57-64 on Capture/Compare/PWM Modules
2. Chapter 11 pages 111-118 on Analog to Digital Converter Module

You also did not answer the homework question I asked in my previous reply.
I can't help you if you can't or won't make even a minimal effort.
 

Thread Starter

damochi

Joined Feb 17, 2006
40
Originally posted by Papabravo@Mar 6 2006, 06:53 PM
damochi

No I am not saying that at all. The frequency of 1.22kHz. is a starting point for constructing a PWM signal. In the end you will do an A/D conversion that will produce a 10 bit result. You will write that 10 bit result to a pair of PWM registers. The resulting average of high and low voltages produced by the PWM will be averaged by the moving coil into a replica of the original analog voltage.

In my previous replies I have told you twice which sections of the datasheet to read. Once again you need to read:
1. Chapter 8 pages 57-64 on Capture/Compare/PWM Modules
2. Chapter 11 pages 111-118 on Analog to Digital Converter Module

You also did not answer the homework question I asked in my previous reply.
I can't help you if you can't or won't make even a minimal effort.
[post=14723]Quoted post[/post]​
please i don't know which homework questions yu talking about please tell me where they are
 

Thread Starter

damochi

Joined Feb 17, 2006
40
Originally posted by damochi@Mar 7 2006, 03:45 PM
please i don't know which homework questions yu talking about please tell me where they are
[post=14752]Quoted post[/post]​

sorry now i understand where to read please don't give up on me yet i've got the circiut diagram for it would yu like to see it ? cos i can scan it and post it to yu
 

Thread Starter

damochi

Joined Feb 17, 2006
40
Originally posted by damochi@Mar 7 2006, 03:54 PM
sorry now i understand where to read please don't give up on me yet i've got the circiut diagram for it would yu like to see it ? cos i can scan it and post it to yu
[post=14753]Quoted post[/post]​
i have read about the timer2 and i think i know the answer to the question yu asked me

i need to set T2CKPS1:T2CKPS0 to 1x for 16

hope thats right and also the PIC i'm using only run at 4MHZ it's a cheaper version

and also i still dunno how i'm going to convert back to analogue from digtal so that the result can be diplayed on the Moving coil
 

Papabravo

Joined Feb 24, 2006
21,158
Originally posted by damochi@Mar 7 2006, 01:06 PM
i have read about the timer2 and i think i know the answer to the question yu asked me

i need to set T2CKPS1:T2CKPS0 to 1x for 16

hope thats right and also the PIC i'm using only run at 4MHZ it's a cheaper version

and also i still dunno how i'm going to convert back to analogue from digtal so that the result can be diplayed on the Moving coil
[post=14755]Quoted post[/post]​
Congratulations -- you are correct
Setting T2CKPS1:T2CKPS0 to 1x will select a divide by 16.
The 1x means that you can use either '10' or '11' in binary for those two bits.

Since your crystal is at 4MHz. instead of 20 MHz., we need to adjust (scale) the values in table 8-3. We do this by dividing each frequency by five(5). Why?
Because the basic system clock of 20 MHz. has been divided by 5 to get 4 MHz.

So -- new header line for table 8-3 in the PIC data sheet

PWM Frequency 244 Hz. 976 Hz. 3.91 kHz 15.624 kHz 31.26 kHz. 41.66 kHz

976 Hz. is pretty close to 1.22 kHz and that is the first PWM frequency I would try. that means we want our prescaler set to 4 and not 16. the setting for a prescaler of 4 should be T2CKPS1:T2CKPS0 to '01' (Check me on this). If this turns out to be unsuitable for any reason then I would try 244 Hz. next.

In order to get the PWM working there are five things you need to do. They are described in section 8.3.3 of the datasheet. this section is immediately above Table 8-3 that we have been talking about.

If you want to post additional information I'll be glad to look at it.
 

Thread Starter

damochi

Joined Feb 17, 2006
40
Originally posted by Papabravo@Mar 7 2006, 11:45 PM
Congratulations -- you are correct
Setting T2CKPS1:T2CKPS0 to 1x will select a divide by 16.
The 1x means that you can use either '10' or '11' in binary for those two bits.

Since your crystal is at 4MHz. instead of 20 MHz., we need to adjust (scale) the values in table 8-3.  We do this by dividing each frequency by five(5).  Why?
Because the basic system clock of 20 MHz. has been divided by 5 to get 4 MHz.

So -- new header line for table 8-3 in the PIC data sheet

PWM Frequency  244 Hz.  976 Hz. 3.91 kHz 15.624 kHz 31.26 kHz. 41.66 kHz

976 Hz. is pretty close to 1.22 kHz and that is the first PWM frequency I would try.  that means we want our prescaler set to 4 and not 16.  the setting for a prescaler of 4 should be T2CKPS1:T2CKPS0 to '01' (Check me on this).  If this turns out to be unsuitable for any reason then I would try 244 Hz. next.

In order to get the PWM working there are five things you need to do.  They are described in section 8.3.3 of the datasheet.  this section is immediately above Table 8-3 that we have been talking about.

If you want to post additional information I'll be glad to look at it.
[post=14758]Quoted post[/post]​
i have attached the circuit diagram please take a look and tell me if on the right track
 
Status
Not open for further replies.
Top