















Preview text:
Cau2ngat: #include #include #include //cấu hình #pragma config OSC=HS #pragma config WDT=OFF #pragma config MCLRE=ON #pragma config PBADEN=OFF #define LED5 PORTBbits.RB7 #define LED4 PORTBbits.RB6 #define LED3 PORTBbits.RB5 #define LED2 PORTBbits.RB4 #define LED1 PORTBbits.RB3
int x;//de ow day de dung dc cho ca ngat nua void ngat_uu_tien_cao(void); void ngat_uu_tien_thap(void);
//Điểm đặt CTCPVN có mức ưu tiên cao UTC, vector 0008H #pragma code uu_tien_cao=0x08 void ngat_cao(void) { ngat_uu_tien_cao(); } #pragma code
#pragma interrupt ngat_uu_tien_cao void ngat_uu_tien_cao(void) { if(INTCONbits.INT0IF==1){ INTCONbits.INT0IF=0; LED5=1;} if(INTCON3bits.INT2IF==1){ INTCON3bits.INT2IF=0; LED5=0;} } void main(){ x=0; ADCON1=0x0f; TRISB=0b00000101; INTCONbits.GIE=1; INTCONbits.INT0IE=1; INTCONbits.INT0IF=0; INTCON2bits.INTEDG0=0; INTCON3bits.INT2IE=1; INTCON3bits.INT2IF=0; INTCON2bits.INTEDG2=0; while(1){ LED1=1; Delay10KTCYx(300); LED1=0; Delay10KTCYx(300); LED2=1; Delay10KTCYx(300); LED2=0; Delay10KTCYx(300); LED3=1; Delay10KTCYx(300); LED3=0; Delay10KTCYx(300); LED4=1; Delay10KTCYx(300); LED4=0; Delay10KTCYx(300);} } Cau1ngat: #include #include #include //cấu hình #pragma config OSC=HS #pragma config WDT=OFF #pragma config MCLRE=ON #pragma config PBADEN=OFF #define LED5 PORTBbits.RB7
int x;//de ow day de dung dc de cho cac ngat cung dung dc void ngat_uu_tien_cao(void); void ngat_uu_tien_thap(void);
//Điểm đặt CTCPVN có mức ưu tiên cao UTC, vector 0008H #pragma code uu_tien_cao=0x08 void ngat_cao(void) { ngat_uu_tien_cao(); } #pragma code
#pragma interrupt ngat_uu_tien_cao void ngat_uu_tien_cao(void) { if(INTCONbits.INT0IF==1){ INTCONbits.INT0IF=0; x++; PORTB=x<<3;} if(INTCON3bits.INT2IF==1){ INTCON3bits.INT2IF=0; x=0; PORTB=x<<3;}
if(x>15){INTCONbits.INT0IE=0;} } void main(){ x=0; ADCON1=0x0f; TRISB=0b00000101; INTCONbits.GIE=1; INTCONbits.INT0IE=1; INTCONbits.INT0IF=0; INTCON2bits.INTEDG0=0; INTCON3bits.INT2IE=1; INTCON3bits.INT2IF=0; INTCON2bits.INTEDG2=0; while(1){ LED5=1; Delay10KTCYx(300); LED5=0; Delay10KTCYx(300);} } Hinh12: Cau1timer: #include #include #include #include //cấu hình #pragma config OSC=HS #pragma config WDT=OFF #pragma config MCLRE=ON #pragma config PBADEN=OFF void main(){ ADCON1=0x0f; TRISE=0b01;
OpenTimer1(TIMER_INT_OFF&T1_SOURCE_INT&T1_PS_1_1&T1_OSC1EN_ON); while(1){
if(PORTEbits.RE0==0){PORTEbits.RE1=0;} else{ WriteTimer1(65536-4000); while(PIR1bits.TMR1IF==0); PIR1bits.TMR1IF=0;
PORTEbits.RE1=~PORTEbits.RE1;}}} Bai2timer: #include #include #include #include //cấu hình #pragma config OSC=HS #pragma config WDT=OFF #pragma config MCLRE=ON #pragma config PBADEN=OFF void main(){ ADCON1=0x0f; TRISE=0b01;
OpenTimer2(TIMER_INT_OFF&T2_PS_1_4&T2_POST_1_10); while(1){ if(PORTEbits.RE0==1){ PR2=125; while(PIR1bits.TMR2IF==0); PIR1bits.TMR2IF=0; PORTEbits.RE1=~PORTEbits.RE1; } else{PR2=250; while(PIR1bits.TMR2IF==0); PIR1bits.TMR2IF=0;
PORTEbits.RE1=~PORTEbits.RE1;}} } Hinhtimer12: Bai1,2counter: #include #include #include #include //cấu hình #pragma config OSC=HS #pragma config WDT=OFF #pragma config MCLRE=ON #pragma config PBADEN=OFF #define LED1 PORTEbits.RE1 #define LED2 PORTEbits.RE0 #define PB1 PORTAbits.RA4 #define PB2 PORTBbits.RB0
const char maled[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; int dem; void main(){ ADCON1=0x0f; TRISE=0; TRISD=0;
OpenTimer0(TIMER_INT_OFF&T0_SOURCE_EXT&T0_16BIT&T0_PS_1_1); while(1){ if(PB2==0){WriteTimer0(0);} else{ dem=ReadTimer0(); LED2=1;LED1=0; PORTD=maled[dem%10]; Delay1KTCYx(10); PORTD=0xFF; LED2=0;LED1=1; PORTD=maled[dem/10%10]; Delay1KTCYx(10); PORTD=0xFF;
if(dem>=20)T0CON=0b00000;}} } Hinh 1,2counter: Bai1usart: #include #include #include #include //cấu hình #pragma config OSC=HS #pragma config WDT=OFF #pragma config MCLRE=ON #pragma config PBADEN=OFF int dem; char x; char m[32]; void main(){ dem=0; TRISC=0x80; TRISD=0b00000001;
OpenUSART(USART_TX_INT_OFF&USART_RX_INT_OFF&USART_ASYNCH_MODE&USART_EIGHT_BIT&US
ART_CONT_RX&USART_BRGH_HIGH,143); while(1){ x=ReadUSART(); if(PORTDbits.RD0==0){ while(PORTDbits.RD0==0); if(dem<25 & x!='a'){ dem++; sprintf(&m[0],"%d",dem); putsUSART(&m[0]); } } } } Hinhusart1: Bai2 usart: #include #include #include #include //cấu hình #pragma config OSC=HS #pragma config WDT=OFF #pragma config MCLRE=ON #pragma config PBADEN=OFF char x; void main(){ TRISC=0x80; TRISB=0b0000000;
OpenUSART(USART_TX_INT_OFF&USART_RX_INT_OFF&USART_ASYNCH_MODE&USART_EIGHT_BIT&US
ART_CONT_RX&USART_BRGH_HIGH,35);
putrsUSART("Pham Ngoc Hoan 2022607230"); while(BusyUSART()); WriteUSART(13); putrsUSART("FExxxx"); while(1){ x=ReadUSART(); if(x=='a') PORTBbits.RB2=1; if(x=='b') PORTBbits.RB2=0; if(x=='c') PORTBbits.RB3=1; if(x=='d') PORTBbits.RB3=0; } } Hinh usart2: baiADC: #include #include #include #include //cấu hình #pragma config OSC=HS #pragma config WDT=OFF #pragma config MCLRE=ON #pragma config PBADEN=OFF int x; void main(){ ADCON1=0b1101; TRISC=0; TRISB=0b10000000;
OpenADC(ADC_FOSC_4&ADC_RIGHT_JUST&ADC_2_TAD,ADC_CH0&ADC_INT_OFF&ADC_VREFPLUS_VD
D&ADC_VREFMINUS_VSS,0b1101); while(1){ if(PORTBbits.RB7==0){ SetChanADC(ADC_CH0); ConvertADC(); while(BusyADC()); x=ReadADC(); PORTC=x; PORTB=x>>9;
if(x>=306 & x<=511) PORTBbits.RB2=1; else PORTBbits.RB2=0;} else{SetChanADC(ADC_CH1); ConvertADC(); while(BusyADC()); x=ReadADC(); PORTC=x; PORTB=x>>9;
if(x>=306 & x<=511) PORTBbits.RB2=1; else PORTBbits.RB2=0;}} } Hinh ADC: BaiPWM: #include #include #include #include #include //cấu hình #pragma config OSC=HS #pragma config WDT=OFF #pragma config MCLRE=ON #pragma config PBADEN=OFF
#pragma config CCP2MX = PORTBE int x; void main(){ x=0; ADCON1=0x0f; TRISB=0b00000001; OpenTimer2(T2_PS_1_16); OpenPWM2(124); SetDCPWM2(250); while(1){ if(PORTBbits.RB0==0){ while(PORTBbits.RB0==0); x++; if(x==1)SetDCPWM2(100); if(x==2)SetDCPWM2(200);
if(x>=3){SetDCPWM2(400);x=0;}}}} Hinh PWM: