Hi everybody!
Been trying this afternoon to get a triangle/sine wave from my PIC by using the PWM-module as a D/A-converter.
Here is the setup:
A PIC16F887 running @ 8 MHz (internal clock)
PWM-module activated (prescaler set at 4, PR2 = 127) and a RC-circuit on the output.
Because it is running at 8 MHz with prescaler at 4 the period-time of the PWM would be:
T = (127 + 1) * 4 * (1/8000000) * 4 = 256 µs
The RC-circuit is chosen as following:
R = 10 kohm
C = 1 µF (electrolyte)
This means that the time-constant is 10ms, which should be enough?
All I get on the output is this:
http://www.youtube.com/watch?v=69ZuGRKI658
And with a other RC-circuit:
http://www.youtube.com/watch?v=EbwOcoxVFPk
What am I doing wrong?
Here is the code:
Been trying this afternoon to get a triangle/sine wave from my PIC by using the PWM-module as a D/A-converter.
Here is the setup:
A PIC16F887 running @ 8 MHz (internal clock)
PWM-module activated (prescaler set at 4, PR2 = 127) and a RC-circuit on the output.
Because it is running at 8 MHz with prescaler at 4 the period-time of the PWM would be:
T = (127 + 1) * 4 * (1/8000000) * 4 = 256 µs
The RC-circuit is chosen as following:
R = 10 kohm
C = 1 µF (electrolyte)
This means that the time-constant is 10ms, which should be enough?
All I get on the output is this:
http://www.youtube.com/watch?v=69ZuGRKI658
And with a other RC-circuit:
http://www.youtube.com/watch?v=EbwOcoxVFPk
What am I doing wrong?
Here is the code:
Rich (BB code):
#include <pic16F887.h>
#include <math.h>
double x, j, y;
unsigned char ad8bit(unsigned char kanal);
void main() {
PWM1CON=0x07;
TRISD=0xFF;
TRISC=0x00;
PR2=0x7F;
CCP1CON=0x0C;
TMR2ON=1;
T2CKPS0 = 1;
T2CKPS1 = 0;
ADCON0=0xC1;
while(1){
x=0;
y=0;
while(x<(2*3.14)){
x = x+0.1;
y=sin(x)+1;
y = y*64;
CCPR1L=(unsigned char)y;
}
}
unsigned char ad8bit(unsigned char kanal){
kanal<<=3;
ADCON0=0xC1 | kanal;
Delay(100);
GODONE=1;
while(GODONE);
return ADRESH;
}
Last edited: