/*
* P2MX683pwmsimple 20240322
* description:generates a PWM signal for LED brightness
* LED brightness is weakly, normal and strongly.
* 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
RST S2 4 GP3/MCLR GP2/CCP1 5 LED PWM OUT
*/
//config:INTOSCIO 8 MHz, 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 //Defined LED2 contrilled by PWM
unsigned int dutyValue=0;
void gpioBegin(); //initialize I/O configuration.
void pwmBegin(void);//Sets up the CCP1 module for PWM mode (active-high).
void pwmDutyWrite(unsigned int duty);//PWM duty cycle based on the provided.
//-------------------------------------------------
void main(void) {
gpioBegin(); //initialization
pwmBegin(); //initialization
while(1){ //loop
dutyValue=10; // duty value range: 0-1023 for 10bitPWM
pwmDutyWrite(dutyValue); //duty1% LED brightness weakly
__delay_ms(1000);
dutyValue=256; // duty value range: 0-1023 for 10bitPWM
pwmDutyWrite(dutyValue); //duty25% LED brightness normal
__delay_ms(1000);
dutyValue=1023; // duty value range: 0-1023 for 10bitPWM
pwmDutyWrite(dutyValue); //duty100% LED brightness strongly
__delay_ms(1000);
}// 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 = 0b011011; // GP0,1,3,4:INPUT,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
PR2 = 255 ; // set PWM period 128uS approx. 7.8KHz
T2CON =0b00000100; // set TMR2ON, Prescaler 1:1
while(!TMR2IF); // wait overflow
}
void pwmDutyWrite(unsigned int duty){
CCPR1L = duty >> 2;
CCP1CON = (duty << 4) | 0x0C;
}
/* result
Data: 8/128bytes 6%
Program: 74/2048words 4%
*/
/*
PWM Period Calculation:
T2CON = 0b00000100
* TMR2ON=1
Timer2_Clock = FOSC / 4 = 8 MHz / 4 = 2 MHz
PR2 Register Value: The code sets PR2 = 255
Calculating PWM Period:
The PWM period (in seconds)
PWM_Period = (PR2 + 1) * Timer2_Clock_Period
PWM_Period = (255 + 1) * (1 / 2 MHz) = 256 * 0.5 ?s = 128 us
* approximately 7.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 pin
is 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
*/