0% found this document useful (0 votes)
59 views

Note CCS

This document provides code for initializing and controlling PWM output on pins RC1 and RC2 of a PIC microcontroller. It defines functions for initializing the PWM frequency, setting the duty cycle, and starting/stopping PWM output. The main function initializes PWM1 and PWM2 at 5000Hz, sets the initial duty cycles to 0%, and starts the PWM output. It then enters a loop to continuously update the duty cycles based on button inputs and delay 50ms between updates.

Uploaded by

Thang Truong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Note CCS

This document provides code for initializing and controlling PWM output on pins RC1 and RC2 of a PIC microcontroller. It defines functions for initializing the PWM frequency, setting the duty cycle, and starting/stopping PWM output. The main function initializes PWM1 and PWM2 at 5000Hz, sets the initial duty cycles to 0%, and starts the PWM output. It then enters a loop to continuously update the duty cycles based on button inputs and delay 50ms between updates.

Uploaded by

Thang Truong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

https://youtu.

be/x0jjlbhxlQU

https://youtu.be/O4zl1qqaFVk

Giáo trình vi điều khiển, sách web


https://tienich123.wordpress.com/2017/10/21/bai-7-mplab-xc8-lap-trinh-doc-adc/

đọc thêm datasheet + Guide_MPLAB trong file TaiLieu (ổ C)


Link lý thuyết: https://deviot.vn/tutorials/pic.22296474/ngat-ngoai.85253280
DEBUG
There are Unresolve includes inside xc.h header,
 Project properties >> XC8 Global Optional >> XC8 Linker >>
Link in C library >> C
 Hoặc là xóa luôn #include đó

Để cho UART chạy thì: Phải cùng Baudrate bằng cách chỉnh set hardware or set software
#define _XTAL_FREQ 20000000
#define TMR2PRESCALE 4
#include // BEGIN CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial
Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM
code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write
protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code
protection off)
//END CONFIG
long freq;
int PWM_Max_Duty()
{
return(_XTAL_FREQ/(freq*TMR2PRESCALE);
}
PWM1_Init(long fre)
{
PR2 = (_XTAL_FREQ/(fre*4*TMR2PRESCALE)) - 1;
freq = fre;
}
PWM2_Init(long fre)
{
PR2 = (_XTAL_FREQ/(fre*4*TMR2PRESCALE)) - 1;
freq = fre;
}
PWM1_Duty(unsigned int duty)
{
if(duty<1024)
{
duty = ((float)duty/1023)*PWM_Max_Duty();
CCP1X = duty & 2;
CCP1Y = duty & 1;
CCPR1L = duty>>2;
}
}
PWM2_Duty(unsigned int duty)
{
if(duty<1024)
{
duty = ((float)duty/1023)*PWM_Max_Duty();
CCP2X = duty & 2;
CCP2Y = duty & 1;
CCPR2L = duty>>2;
}
}
PWM1_Start()
{
CCP1M3 = 1;
CCP1M2 = 1;
#if TMR2PRESCALAR == 1
T2CKPS0 = 0;
T2CKPS1 = 0;
#elif TMR2PRESCALAR == 4
T2CKPS0 = 1;
T2CKPS1 = 0;
#elif TMR2PRESCALAR == 16
T2CKPS0 = 1;
T2CKPS1 = 1;
#endif
TMR2ON = 1;
TRISC2 = 0;
}
PWM1_Stop()
{
CCP1M3 = 0;
CCP1M2 = 0;
}
PWM2_Start()
{
CCP2M3 = 1;
CCP2M2 = 1;
#if TMR2PRESCALE == 1
T2CKPS0 = 0;
T2CKPS1 = 0;
#elif TMR2PRESCALE == 4
T2CKPS0 = 1;
T2CKPS1 = 0;
#elif TMR2PRESCALE == 16
T2CKPS0 = 1;
T2CKPS1 = 1;
#endif
TMR2ON = 1;
TRISC1 = 0;
}
PWM2_Stop()
{
CCP2M3 = 0;
CCP2M2 = 0;
}
void main()
{
unsigned int i=0,j=0;
PWM1_Init(5000);
PWM2_Init(5000);
TRISD = 0xFF;
TRISB = 0;
PWM1_Duty(0);
PWM2_Duty(0);
PWM1_Start();
PWM2_Start();
do
{
if(RD0 == 0 && i<1000)
i=i+10;
if(RD1 == 0 && i>0)
i=i-10;
if(RD2 == 0 && j<1000)
j=j+10;
if(RD3 == 0 && j>0)
j=j-10;
PWM1_Duty(i);
PWM2_Duty(j);
__delay_ms(50);
}while(1);
}

You might also like