* P2MX683sinpwm10bit 20240328
* description:generates sin wave 50Hz using 10bitPWM(CCP) 160PWM
* board: Curiosity development with PIC12F683 5V INTOSC 8MHz
* MPU; PIC12F683 DIP8P INTOSC8MHz
* Flash:2KW, RAM:128B,EEPROM 256Byte
* language: C XC8 v2.46 C99
* IDE: MPLABX v6.20, XC8 v2.46
* debug tool: PICkit3/PICkit4
//------------------------------------------------
* PIC12F683 DIP 8Pin on Curiosity development board
5V 1 VDD VSS 8 GND
2 GP5 GP0 7 ICSPDAT
3 GP4 GP1 6 ICSPCLK LED
RST S2 4 GP3/MCLR GP2/CCP1 5 LED PWM OUT LPf 1k/1uf
*/
//INTOSCIO 8MHz Disable watchdog timer, Enable master clear on reset
#pragma config FOSC=INTOSCIO, WDTE=OFF, PWRTE=OFF, MCLRE=ON
#pragma config CP= OFF, CPD=OFF, BOREN=ON, IESO=ON, FCMEN=ON
#include
#define _XTAL_FREQ 8000000
#define LED2 GP2 // LED2 controlled by 160PWM
#define LED1 GP1 // LED1 run monitor 50Hz
unsigned int dutyValue=0;
void gpioBegin(); //initialize I/O configuration.
void pwmBegin(void);//Sets up the CCP1 module for PWM mode (active-high).
//-------------------------------------------------
const unsigned int sinValue[] ={
498, 517, 537, 556, 575, 595, 614, 633, 651, 670,
688, 706, 724, 741, 758, 774, 790, 806, 821, 836,
850, 863, 876, 889, 900, 912, 922, 932, 941, 950,
958, 965, 971, 977, 982, 986, 989, 992, 994, 995,
996, 995, 994, 992, 989, 986, 982, 977, 971, 965,
958, 950, 941, 932, 922, 912, 900, 889, 876, 863,
850, 836, 821, 806, 790, 774, 758, 741, 724, 706,
688, 670, 651, 633, 614, 595, 575, 556, 537, 517,
498, 478, 458, 439, 420, 400, 381, 362, 344, 325,
307, 289, 271, 254, 237, 221, 205, 189, 174, 159,
145, 132, 119, 106, 95, 83, 73, 63, 54, 45,
37, 30, 24, 18, 13, 9, 6, 3, 1, 0,
0, 0, 1, 3, 6, 9, 13, 18, 24, 30,
37, 45, 54, 63, 73, 83, 95, 106, 119, 132,
145, 159, 174, 189, 205, 221, 237, 254, 271, 289,
307, 325, 344, 362, 381, 400, 420, 439, 458, 478,
};
unsigned char count = 0;
void main(void) {
gpioBegin(); // Initializes General Purpose Input/Output (GPIO) pins.
pwmBegin(); // CCP1 for PWM mode with a period of approximately 8KHz.
while(1){ //Enters an infinite loop
while(!TMR2IF); //Waits for Timer2 overflow interrupt flag
TMR2IF=0; //Clears the flag.
count++; //Increments a counter (count).
if(count >= 160){
count = 0; // Resets count 8KHz/50Hz=160, 160PWM
LED1=!LED1;
}
CCPR1L = sinValue[count]>>2; //Sets the duty cycle MSB:8bit
CCP1CONbits.DC1B=sinValue[count]&0x03; //PWM Duty Cycle LSB:2bit
}// end while
}// end main
//-----------------------------------------------
void gpioBegin(void){
OSCCON = 0x70; // Sets internal oscillator to 8 MHz (OSCCON)
ANSEL = 0b00000000; // Sets all pins to digital inputs
GPIO = 0b000000; // GPIO clear
TRISIO = 0b011001; // GP0,3,4:INPUT,GP1,GP2,GP5;OUTPUT
CMCON0 = 0x07; // Enables comparator (CMCON0) (potentially for future use)
}
void pwmBegin(void){
TRISIO2 = 0; // GP2:PWM OUTPUT
CCP1CON = 0b00001100; // PWM mode active-high
CCPR1L = 0; // duty 0
PR2 = 249 ; // set PWM period approx. 8KHz
T2CON =0b00000100; // set TMR2ON, Prescaler 1:1
//T2CON =0b00000101; // set TMR2ON, Prescaler 1:1
while(!TMR2IF); // wait overflow
}
/* result
Data: 9/128bytes 7%
Program: 446/2048words 22%
*/
/*
PWM Period Calculation:
T2CON = 0b00000100
* TMR2ON=1 * prescale:1
Timer2_Clock = FOSC / 4 / 1 (prescale)= 8 MHz / 4 /1 = 2 MHz
PR2 Register Value: The code sets PR2 = 249
Calculating PWM Period:
The PWM period (in seconds)
PWM_Period = (PR2 + 1) * Timer2_Clock_Period
PWM_Period = (249 + 1) * (1 /2 MHz) = 250 * 0,5 us= 125us
* approximately 8kHz
*/
/*
CCP1CON: CCP1 CONTROL REGISTER
? ? DC1B1 DC1B0 CCP1M3 CCP1M2 CCP1M1 CCP1M0
bit 7-6 Unimplemented: Read as ?0?
bit 5-4 DC1B<1:0>: PWM Duty Cycle Least Significant bits
Capture mode:
Unused.
Compare mode:
Unused.
PWM mode:These bits are the two LSbs of the PWM duty cycle.
The eight MSbs are found in CCPR1L.
bit 3-0 CCP1M<3:0>: CCP Mode Select bits
0000 = Capture/Compare/PWM off (resets CCP module)
0001 = Unused (reserved)
0010 = Unused (reserved)
0011 = Unused (reserved)
0100 = Capture mode, every falling edge
0101 = Capture mode, every rising edge
0110 = Capture mode, every 4th rising edge
0111 = Capture mode, every 16th rising edge
1000 = Compare mode, set output on match (CCP1IF bit is set)
1001 = Compare mode, clear output on match (CCP1IF bit is set)
1010 = Compare mode, generate software interrupt on match (CCP1IF bit is set,
* CCP1 pinis unaffected)
1011 = Compare mode, trigger special event
* (CCP1IF bit is set, TMR1 is reset and A/D
conversion is started if the ADC module is enabled. CCP1 pin is unaffected.)
110x = PWM mode active-high
111x = PWM mode active-low
*/
/*
T2CON: TIMER 2 CONTROL REGISTER
? TOUTPS3 TOUTPS2 TOUTPS1 TOUTPS0 TMR2ON T2CKPS1 T2CKPS0
bit 6-3 TOUTPS<3:0>: Timer2 Output Postscaler Select bits
0000 = 1:1 Postscaler
0001 = 1:2 Postscaler
0010 = 1:3 Postscaler
0011 = 1:4 Postscaler
0100 = 1:5 Postscaler
0101 = 1:6 Postscaler
0110 = 1:7 Postscaler
0111 = 1:8 Postscaler
1000 = 1:9 Postscaler
1001 = 1:10 Postscaler
1010 = 1:11 Postscaler
1011 = 1:12 Postscaler
1100 = 1:13 Postscaler
1101 = 1:14 Postscaler
1110 = 1:15 Postscaler
1111 = 1:16 Postscaler
bit 2 TMR2ON: Timer2 On bit
1 = Timer2 is on
0 = Timer2 is off
bit 1-0 T2CKPS<1:0>: Timer2 Clock Prescale Select bits
00 = Prescaler is 1
01 = Prescaler is 4
1x = Prescaler is 16
*/