0% found this document useful (0 votes)
41 views37 pages

#Include #Include

The document contains code for several microcontroller programs that use timers and PWM to control LEDs. The programs: 1. Use timers 0, 1 and PWM to blink LEDs at different frequencies (2500Hz, 1400Hz) 2. Use timer 0 to display a scrolling male character on LEDs 3. Use timer 1 to display a scrolling character array on LEDs 4. Implement PWM with different duty cycles (120, 180, 360) controlled by buttons to vary the brightness of an LED. 5. Similarly, another program uses PWM with varying duty cycles (120, 280, 360) controlled by a button to adjust LED brightness.

Uploaded by

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

#Include #Include

The document contains code for several microcontroller programs that use timers and PWM to control LEDs. The programs: 1. Use timers 0, 1 and PWM to blink LEDs at different frequencies (2500Hz, 1400Hz) 2. Use timer 0 to display a scrolling male character on LEDs 3. Use timer 1 to display a scrolling character array on LEDs 4. Implement PWM with different duty cycles (120, 180, 360) controlled by buttons to vary the brightness of an LED. 5. Similarly, another program uses PWM with varying duty cycles (120, 280, 360) controlled by a button to adjust LED brightness.

Uploaded by

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

#include<p18f4520.

h>
#include<delays.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF

#define PB1 PORTBbits.RB0


void main(void)
{
TRISB=0b00000001;
TRISD=0x00;
ADCON1=0x0f;
while(1)
{
if(PB1==1)
{
PORTD=0b00000001;
Delay10KTCYx(50);
PORTD=0b00000010;
Delay10KTCYx(50);
PORTD=0b00000100;
Delay10KTCYx(50);
PORTD=0b00001000;
Delay10KTCYx(50);//THOI GIAN TRE = 10000 *(4/Fosc) * Unit
}
else PORTD=0;
}
}
#include<p18f4520.h>
#include<delays.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF

#define PB1 PORTBbits.RB0

char i;
void main(void)
{
TRISB=0x01;
TRISD=0x00;
ADCON1=0x0f;

PORTD=0;
while(1)
{
if(PB1==0)
{
while(PB1==0);
i++;
}
if(i==1) PORTD=0b00000001;
if(i==2) PORTD=0b00000010;
if(i==3) PORTD=0b00000100;
if(i==4) PORTD=0b00001000;
if(i>=5) PORTD=0b00001111;
}
}
#include<p18f4520.h>
#include<delays.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF

#define PB1 PORTBbits.RB0

char i;
void main(void)
{
TRISB=0x01;
TRISD=0x00;
ADCON1=0x0f;

PORTD=0;
while(1)
{
if(PB1==1)
{
while(PB1==1);
i++;
}
PORTD=(i%16);
}
}
#include<p18f4520.h>
#include<delays.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF

#define PB1 PORTBbits.RB0


#define PB2 PORTBbits.RB1

void main(void)
{
TRISB=0x03;
TRISD=0X00;
ADCON1=0x0f;
while(1)
{
if(PB1==0 && PB2==0) PORTD=0b00000001;
if(PB1==0 && PB2==1) PORTD=0b00000010;
if(PB1==1&& PB2==0) PORTD=0b000000100;
if(PB1==1&& PB2==1) PORTD=0b000001000;
}
}
#include<p18f4520.h>
#include<delays.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF

#define PB1 PORTBbits.RB0


#define PB2 PORTBbits.RB1
char i;

void main(void)
{
TRISB=0x03;
TRISD=0x00;
ADCON1=0x0f;
while(1)
{
if(PB1==1) PORTD=0;
else
{
if(PB2==0)
{
while(PB2==0);
i++;
}
if(i==0) PORTD=0;
if(i==1) PORTD=0b00001000;
if(i==2) PORTD=0b00000100;
if(i>=3) PORTD=0b00001100;
}
}
}
#include<p18f4520.h>
#include<delays.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF

#define LED1 PORTCbits.RC2


#define LED2 PORTCbits.RC4

char i;

void ngat_ngoai(void);
//Ði?m d?t chuong trình ph?c v? ng?t có m?c uu tiên cao (vector 0008H)
#pragma code uu_tien_cao = 0x08
// N?u là chuong trình ph?c v? ng?t có m?c uu tiên th?p (vector 0018H):
// #pragma code uu_tien_thap = 0x18
//Vi?t 1 chuong trình con (có tên tùy ch?n) d? g?i t?i CTCPVN
void ngat_cao(void)
{
ngat_ngoai(); //g?i d?n CTCPVN
}
//Ði?m vi?t CTCPVN
#pragma code
#pragma interrupt ngat_ngoai
void ngat_ngoai(void)
//Noi dung cua CTCPVN
{
if(INTCON3bits.INT1IF==1)
{
LED1=0;
for(i=0;i<20;i++)
{
LED2=~LED2;
Delay10KTCYx(50);
}
INTCON3bits.INT1IF=0;
}
}
void main(void)
{
TRISB=0xff;
TRISC=0x00;
ADCON1=0x0f;

//IPEN, GIE, PEIE, IE, IF ,IP, EDG - INT1


RCONbits.IPEN=0;// KHONG UU TIEN NGAT
INTCONbits.GIE=1;// CHO PHEP CAC NGAT KHONG SU DUNG MAT LA
INTCONbits.PEIE=1;// CHO PHEP NGAT NGOAI VI
INTCON3bits.INT1IE=1;// CHO PHEP NGAT NGOAI INT1
INTCON3bits.INT1IF=0;// KHONG XUAT HIEN CO NGAT
INTCON3bits.INT1IP=1;// UU TIEN CAO
INTCON2bits.INTEDG1=1;;// suon duong

while(1)
{
LED1=~LED1;
Delay10KTCYx(50); //thoi gian tre = 10000 * (4/Fosc)* Unit
}
}
#include<p18f4520.h>
#include<delays.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF

#define LED4 PORTCbits.RC7

char i;

void ngat_ngoai(void);
//Ði?m d?t chuong trình ph?c v? ng?t có m?c uu tiên cao (vector 0008H)
#pragma code uu_tien_cao = 0x08
// N?u là chuong trình ph?c v? ng?t có m?c uu tiên th?p (vector 0018H):
// #pragma code uu_tien_thap = 0x18
//Vi?t 1 chuong trình con (có tên tùy ch?n) d? g?i t?i CTCPVN
void ngat_cao(void)
{
ngat_ngoai(); //g?i d?n CTCPVN
}
//Ði?m vi?t CTCPVN
#pragma code
#pragma interrupt ngat_ngoai
void ngat_ngoai(void)
//Noi dung cua CTCPVN
{
i++;
PORTC=(i<<4);
INTCONbits.INT0IF=0;
}
void main(void)
{
TRISB=0xff;
TRISC=0x00;
ADCON1=0x0f;

//IPEN, GIE, PEIE, IE, IF, EDG - INT0


RCONbits.IPEN=0;// KHONG UU TIEN NGAT
INTCONbits.GIE=1;// CHO PHEP TAT CA CAC NGAT SU DUNG MAT LA
INTCONbits.PEIE=1;// CHO PHEP TAT CA CAC NGAT SU DUNG MAT LA
INTCONbits.INT0IE=1;// CHO PHEP NGAT NGOAI INT0
INTCONbits.INT0IF=0;// KO XUAT HIEN NGAT NGOAI
INTCON2bits.INTEDG0=1;// SUON DUONG
while(1)
{
LED4=~LED4;
Delay10KTCYx(50);
}
}
/*
Timer0
tao F = 2500Hz => T = 1/2500
Fosc = 12MHz
TimerCount = Delay * Fosc /4/ PV
= 1/5000 * 12000000 /4 / PV
= 600/PV
PV = 1
TimerCount = 600 (<65535:16 bit; <255: 8bit)

*/
#include<p18f4520.h>
#include<delays.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config LVP = OFF
#pragma config WDT = OFF
#define XUNG PORTCbits.RC2
void main(void)
{
TRISC=0x00;
ADCON1=0x0f;
// khoi tao timer0
T0CON=0b00001000;
while(1)
{
// tao tre
T0CONbits.TMR0ON=0;
TMR0H= (65536-600)/256;
TMR0L= (65536-600)%256;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);// CHO CO TRAN
INTCONbits.TMR0IF=0; // XOA CO TRAN
XUNG=~XUNG;
}
}
/*
F=1400Hz => T=1/1400
Fosc = 12 MHz
TIMER 1
Timer Count = Delay * Fosc /4 / PV
= 1/2800 * 12000000/4 /PV
= 1071
PV=1
Timer Count = 1071

*/
#include<p18f4520.h>
#include<delays.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config LVP = OFF
#pragma config WDT = OFF

#define XUNG PORTBbits.RB2

void main(void)
{
TRISB=0x00;
ADCON1=0x0f;
//khoi tao timer 1
T1CON=0b11000000;

while(1)
{
T1CONbits.TMR1ON=0;
TMR1H=(65536 -1071)/256;
TMR1L=(65536 -1071)%256;
T1CONbits.TMR1ON=1;
while(PIR1bits.TMR1IF==0);// cho co ngat
PIR1bits.TMR1IF=0; // xoa co ngat
XUNG=~XUNG;
}
}
#include<p18f4520.h>
#include<delays.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config LVP = OFF
#pragma config WDT = OFF

int i;

char maled[10]={0b11000000,0b11111001,
0b10100100,0b10110000,
0b10011001,0b10010010,
0b10000010,0b11111000,
0b10000000,0b10010000};

void main(void)
{
TRISA=0xff;
TRISB=0x00;
ADCON1=0x0f;
// khoi tao timer
T0CON=0b11111000;
TMR0L=0;

while(1)
{
i=TMR0L;
PORTB=maled[i%10];
}
}
#include<p18f4520.h>
#include<delays.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF

char i;

char ma_led[]=
{0b11000000,0b11111001,
0b10100100,0b10110000,
0b10011001,0b10010010,
0b10000010,0b11111000,
0b10000000,0b10010000};
void main(void)
{
// khoi tao vao ra
TRISC=0xff;
TRISD=0x00;
ADCON1=0x0f;
// khoi tao timer1
T1CON=0b10000111;
TMR1L=0;
while(1)
{
i=TMR1L;
PORTD=ma_led[i%10];
}
}
/*
Fosc=12MHz
Tpwm=200uS
Tpwm =(PR2+1)*4*Tosc*PV
200uS =(PR2+1)*4*1/(12MHz)*PV
600 =(PR2+1)*PV

PR2 = 149 (<=255)


PV = 4 (1,4,16)

Tpwm = Ton + Toff


Ton = 2/8 Toff => Ton = 2/10 Tpwm = 40uS = dutycycle * Tosc * PV = dutycycle /3
// sau bat nguon
Ton(DT)=2/10Tpwm =40uS
dutycycle=120
//nhan KEY1
Ton(DT)=3/10Tpwm =60uS
dutycycle=180
//nhan KEY2
Ton(DT)=6/10Tpwm =120uS
dutycycle=360
*/

#include<p18f4520.h>
#include<delays.h>
#include<pwm.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config CCP2MX=PORTC

#define KEY1 PORTBbits.RB0


#define KEY2 PORTBbits.RB3

void main(void)
{
//khoi tao vao ra
TRISB=0xff;
TRISC=0x00;
ADCON1=0x0f;
//thiet lap PWM, CHU KY
OpenPWM2(149);
T2CONbits.T2CKPS1=0;
T2CONbits.T2CKPS0=1;
//thiet lap do rong
SetDCPWM2(120);
while(1)
{
if(KEY1==0)
SetDCPWM2(180);
if(KEY2==0)
SetDCPWM2(360);
}
}
/*
F= 4MHz
Tpwm = 100uS
Tpwm = (PR2+1)*4*Tosc*PV
100uS=(PR2+1)*4*(1/4MHz)*PV
100=(PR2+1)*PV
PV =1
PR2=99
Tpwm = Ton + Toff
Tr?ng thái ban d?u Ton=9Toff=90µs = dutycycle * Tosc * PV = dutycycle /4
//trang thai ban dau
Ton(DT) = 90
dutycycle = 360
// nhan Key lan 1
Ton(DT) = 30
dutycycle = 120
// nhan key lan 2
Ton(DT) = 70
dutycycle = 280
// nhan key lon hon 3
Ton(DT) = 90
dutycycle = 360
*/
#include<p18f4520.h>
#include<delays.h>
#include<pwm.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config CCP2MX = PORTC

char i;

#define KEY PORTBbits.RB0


void main(void)
{
// khoi tao vao ra
TRISB=0xff;
TRISC=0x00;
ADCON1=0x0f;
// thiet lap pwm
OpenPWM2(99);
T2CONbits.T2CKPS1=0;
T2CONbits.T2CKPS0=0;
//thiet lap do rong
SetDCPWM2(360);
while(1)
{
if(KEY==0)
{
while(KEY==0);
i++;
}
if(i==1) SetDCPWM2(120);
if(i==2) SetDCPWM2(280);
if(i>2) SetDCPWM2(360);

}
}
#include<p18f4520.h>
#include<delays.h>
#include<adc.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF

#define LED1 PORTEbits.RE1

int adc;

float dienap;

void main(void)
{
// khoi tao vao ra
TRISA=0xff;
TRISC=0x00;
TRISD=0X00;
TRISE=0x00;
ADCON1=0x0D;
// KHOI TAO ADC
OpenADC( ADC_FOSC_8&ADC_RIGHT_JUST&ADC_8_TAD,

ADC_CH1&ADC_INT_OFF&ADC_VREFPLUS_VDD&ADC_VREFMINUS_VSS,
13);
while(1)
{
ConvertADC();
while(BusyADC());
adc=ReadADC();

PORTC=adc/256; //8 BIT CAO (2BIT)


PORTD=adc%256; //8 BIT THAP

dienap=((float) adc*5)/1023;
if(dienap>=3)
LED1=1;
else
LED1=0;
}
}
#include<p18f4520.h>
#include<delays.h>
#include<adc.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF

int adc;
float dienap;

#define LED11 PORTEbits.RE1

void main(void)
{
// khoi tao vao ra
TRISA=0xff;
TRISC=0x00;
TRISD=0x00;
TRISE=0x00;
ADCON1=0x0A;
// khoi tao adc
OpenADC( ADC_FOSC_8&ADC_RIGHT_JUST&ADC_8_TAD,

ADC_CH4&ADC_INT_OFF&ADC_VREFPLUS_VDD&ADC_VREFMINUS_VSS,
10);
while(1)
{
ConvertADC();
while(BusyADC());
adc=ReadADC();

PORTC=adc/256;
PORTD=adc%256;

dienap=((float)adc*5)/1023;
if(dienap>=2 && dienap<=3)
LED11=1;
else LED11=0;
}
}
#include<p18f4520.h>
#include<delays.h>
#include<usart.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config LVP = OFF
#pragma config WDT = OFF
char xx;
void main(void)
{
//Khoi tao vao ra
TRISC=0b10111111;
TRISD=0b00000000;
ADCON1=0x0f;
//thiet lap usart
OpenUSART( USART_TX_INT_OFF&USART_RX_INT_OFF&USART_ASYNCH_MODE
&USART_EIGHT_BIT&USART_CONT_RX&USART_BRGH_HIGH,
38);
//T?c d? baud=Fosc/ (16 * (spbrg+ 1))
//19200 = 12000000/(16 * (spbrg+ 1))
//spbrg=38
//putrsUSART("DAI HOC CONG NGHIEP HA NOI");
while(BusyUSART());
WriteUSART(0x41);
while(BusyUSART());
WriteUSART(0x61);
while(1)
{
xx=ReadUSART();
PORTD=xx;
}
}
#include<p18f4520.h>
#include<usart.h>

#pragma config OSC = HS


#pragma config MCLRE = ON
#pragma config LVP = OFF
#pragma config WDT = OFF

char xx;

#define LED1 PORTDbits.RD0

void main(void)
{
TRISC=0b10111111;
TRISD=0x00;
ADCON1=0x0f;
//thiet lap usart
OpenUSART( USART_TX_INT_OFF&USART_RX_INT_OFF&USART_ASYNCH_MODE
&USART_EIGHT_BIT&USART_CONT_RX&USART_BRGH_HIGH,
71);
//T?c d? baud=Fosc/ (16 * (spbrg+ 1))
// 9600=11059200/ (16 * (spbrg+ 1))
//spbrg= 71
putrsUSART("START");
while(1)
{
xx=ReadUSART();
if(xx=='k') LED1=1;
else LED1=0;
}
}

You might also like