b. Sơ đồ khối
Sơ đồ khối:
CODE:
#include <pic.h> #include <pic16f877a.h> #include <xc.h> #include <stdio.h> #include <stdlib.h>
#define _XTAL_FREQ 20000000
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF #pragma config CP = OFF
int d = 0;
void hieuung1()
{
delay_ms(500);
delay_ms(500);
}
void hieuung2()
{
PORTD = 0b10000000;
delay_ms(500); for(int j = 0; j<7 ;j++)
{
PORTD >>=1;
delay_ms(500);
}
}
void hieuung3 ()
{
char Mang2[] = {0b10000001, 0b11000011, 0b11100111, 0xff};
for(int j =0;j<4;j++)
{
PORTD = Mang2[j];
delay_ms (500);
}
}
void main(void)
{
while(1)
{
if(d==1)
{
hieuung1();
}
if(d==2)
{
hieuung2();
}
if(d==3)
{
hieuung3();
}
}
}
void interrupt NGAT(void)
{
{
d++;
{
d=1;
}
}
}
Mô phỏng
Mục đích
Sơ đồ khối:
CODE:
#include <pic.h> #include <pic16f877a.h> #include <xc.h> #include <stdio.h> #include <stdlib.h>
#define _XTAL_FREQ 20000000
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF #pragma config CP = OFF
void delay_timer0_5ms(unsigned int time)
{
{
OPTION_REGbits.PS1 = 1;
}
}
void hienthi(unsigned int h,unsigned int m,unsigned int s)
{
h1 = h/10; h2 = h%10;
m1=m/10; m2=m%10;
s1=s/10; s2=s%10;
{
PORTDbits.RD1=0; PORTB = MALED[h1];
delay_timer0_5ms(2); PORTDbits.RD1=1; PORTDbits.RD2=0; PORTB = MALED[h2];
delay_timer0_5ms(1); PORTDbits.RD2=1;
PORTDbits.RD3=0; PORTB = MALED[m1];
delay_timer0_5ms(2); PORTDbits.RD3=1; PORTDbits.RD4=0; PORTB = MALED[m2];
delay_timer0_5ms(2); PORTDbits.RD4=1;
PORTDbits.RD5=0; PORTB = MALED[s1];
delay_timer0_5ms(2); PORTDbits.RD5=1; PORTDbits.RD6=0; PORTB = MALED[s2];
delay_timer0_5ms(1); PORTDbits.RD6=1;
}
}
void main(void)
{
TRISB = 0x00; TRISD = 0x00; TRISC = 0x00;
unsigned int gio = 0,giay= 0,phut = 0; while(1)
{
{
}
{
phut=0; gio++;
}
{
gio = 0;
}
}
}
Mô phỏng:
#define _XTAL_FREQ 20000000
#define RS RD2 #define EN RD3 #define D4 RD4 #define D5 RD5 #define D6 RD6 #define D7 RD7
#include <pic.h> #include <pic16f877a.h> #include <xc.h> #include <stdio.h> #include <stdlib.h>
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF #pragma config CP = OFF
void Lcd_Port(char a)
{
if(a & 1)
D4 = 1;
else
D4 = 0;
if(a & 2)
D5 = 1;
else
D5 = 0;
if(a & 4)
D6 = 1;
else
D6 = 0;
if(a & 8)
D7 = 1;
else
}
D7 = 0;
void Lcd_Cmd(char a)
{
EN = 0;
}
void Lcd_Clear()
{
Lcd_Cmd(0);
}
void Lcd_Set_Cursor(char a, char b)
{
char temp,z,y;
{
y = temp & 0x0F; Lcd_Cmd(z); Lcd_Cmd(y);
}
{
temp = 0xC0 + b - 1; z = temp>>4;
y = temp & 0x0F; Lcd_Cmd(z); Lcd_Cmd(y);
}
}
void Lcd_Init()
{
Lcd_Port(0x00);
delay_ms(20); Lcd_Cmd(0x03);
delay_ms(5); Lcd_Cmd(0x03);
delay_ms(11); Lcd_Cmd(0x03);
///////////////////////////////////////////////////// Lcd_Cmd(0x02);
Lcd_Cmd(0x02); Lcd_Cmd(0x08); Lcd_Cmd(0x00); Lcd_Cmd(0x0C); Lcd_Cmd(0x00); Lcd_Cmd(0x06);
}
void Lcd_Write_Char(char a)
{
char temp,y;
y = a&0xF0;
Lcd_Port(y>>4);
delay_us(40); EN = 0;
Lcd_Port(temp); EN = 1;
delay_us(40); EN = 0;
}
void Lcd_Write_String(char *a)
{
int i;
Lcd_Write_Char(a[i]);
}
void Lcd_Shift_Right()
{
Lcd_Cmd(0x01); Lcd_Cmd(0x0C);
}
void Lcd_Shift_Left()
{
Lcd_Cmd(0x01); Lcd_Cmd(0x08);
}
void main(void)
{
TRISA4 = 1;
Lcd_Init(); while (1)
{
sprintf(b,”Xung dem: %d”,dem); Lcd_Set_Cursor(1,1);
dem=TMR0;
{
TMR0=0;
dem=0;
}
}
}
#define _XTAL_FREQ 20000000
#include <pic.h> #include <pic16f877a.h> #include <xc.h> #include <stdio.h> #include <stdlib.h>
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF
#pragma config WRT = OFF #pragma config CP = OFF void Timer1_Init()
{
}
void interrupt timer1()
{
if(PIR1bits.TMR1IF ==1)
{
TMR1L=3035&0X00FF;
PIR1bits.TMR1IF = 0;
}
}
void main()
{
TRISBbits.TRISB0 = 0;
PORTBbits.RB0 = 0;
Timer1_Init(); while(1)
{}
}
#include <pic.h> #include <pic16f877a.h> #include <xc.h> #include <stdio.h> #include <stdlib.h>
#define _XTAL_FREQ 20000000
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF #pragma config CP = OFF
#define dx1 RB0 #define dv1 RB1 #define dd1 RB2 #define dx2 RB3 #define dv2 RB4 #define dd2 RB5 #define dx3 RB6 #define dv3 RB7 #define dd3 RC0 #define dx4 RC1 #define dv4 RC2 #define dd4 RC3
#define bat 1
#define tat 0
void delay_timer2(unsigned int time)
{
while(time--)
{
T2CONbits.T2CKPS1 = 1;
T2CONbits.TOUTPS3 = 0;
T2CONbits.TOUTPS2 = 0;
T2CONbits.TOUTPS1 = 0;
void main(void)
{
TRISB = TRISC = 0x00; PORTB = PORTC = 0x00;
while(1)
{
{
delay_timer2(1000);
}
{
dx1 = dx3 = dv1 = dv3 = dx2 = dx4 = dd2 = dd4 =tat; dv2 = dv4 = dd1 = dd3 = bat;
delay_timer2(1000); dem++;
}
{
dx2 = dx4 = dd1 = dd3 = dv1 = dv2 = dv3 = dv4 = tat; dx1 = dx3 = dd2 = dd4 = bat;
delay_timer2(1000); dem++;
}
{
dx2 = dx4 = dx1 = dx3 = dv2 = dv4 = dd1 = dd3 = tat; dv1 = dv3 = dd2 = dd4 = bat;
delay_timer2(1000); dem++;
}
{
dem = 0;
}
}
}
#include <pic.h> #include <pic16f877a.h> #include <xc.h> #include <stdio.h> #include <stdlib.h>
#define _XTAL_FREQ 20000000
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF #pragma config CP = OFF
#define RS RD2 #define EN RD3 #define D4 RD4 #define D5 RD5 #define D6 RD6 #define D7 RD7
void ADC_Init(void)
{
ADCON1bits.ADCS2 = 0;
ADCON0bits.ADCS1 = 1;
ADCON0bits.CHS2 = 0;
ADCON0bits.CHS1 = 0;
ADCON1bits.PCFG3 = 1;
ADCON1bits.PCFG2 = 1;
ADCON1bits.PCFG1 = 1;
ADCON0bits.ADON = 1;
}
unsigned int ADC_Read()
{
}
void main(void)
{
PORTD = 0x00; TRISBbits.TRISB0 = 0;
PORTBbits.RB0 = 0;
TRISAbits.TRISA0 = 1;
PORTAbits.RA0 = 1;
Lcd_Init(); ADC_Init(); char str[20]; while(1)
{
{
Lcd_Set_Cursor(1,1); Lcd_Write_String(str);
Lcd_Write_String("C"); Lcd_Set_Cursor(2,1); Lcd_Write_String(" GOOD! ");
}
else
{
Lcd_Set_ Cursor(1,1); Lcd_Write_String(“ WARNING! ”); Lcd_Set_Cursor(2,1)
Lcd_Write_String(“ NHIET DO CAO”);
}
}
}
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF #pragma config CP = OFF
#include <xc.h> #include <pic.h> #include <stdio.h> #include <stdlib.h> #include <pic16f877a.h>
#define _XTAL_FREQ 20000000
#define PRESCALE 4
long fre1,fre2;
void PWM1_Init(long fre)
{
}
void PWM2_Init(long fre)
{
PR2 =(_XTAL_FREQ / (4*fre*PRESCALE)-1);
fre2=fre;
}
void PWM1_Start()
{
CCP1CONbits.CCP1M2=1;
}
void PWM2_Start()
{
CCP2CONbits.CCP2M2 = 1;
CCP2CONbits.CCP2M3 = 1;
T2CONbits.TMR2ON = 1;
TRISCbits.TRISC1 = 0;
}
void Duty_Cycle(unsigned int cpp, unsigned int duty)
{
{
}
{
duty = ((float)duty/1023) * (_XTAL_FREQ/(fre2*PRESCALE)); CCP2CONbits.CCP2X = duty&2;
CCP2CONbits.CCP2Y = duty&1; CCPR2L = duty>>2;
}
}
void PWM1_Stop()
{
CCP1CONbits.CCP1M2 = 0;
CCP1CONbits.CCP1M1 = 0;
CCP1CONbits.CCP1M0 = 0;
}
void PWM2_Stop()
{
CCP2CONbits.CCP2M2 = 0;
CCP2CONbits.CCP2M1 = 0;
CCP2CONbits.CCP2M0 = 0;
}
void main(void)
{
PWM2_Init(10000); TRISBbits.TRISB0 = 1;
TRISBbits.TRISB1 = 1;
TRISBbits.TRISB2 = 1; PORTB = 0xFF;
while(1)
{
{
PWM1_Start();
}
else if(RB1 == 0)
{
PWM2_Start();
}
else if(RB3 == 0)
{
PWM2_Stop();
}
}
}
#include <pic.h> #include <pic16f877a.h> #include <xc.h> #include <stdio.h> #include <stdlib.h>
#define _XTAL_FREQ 20000000
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = ON #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF #pragma config CP = OFF
void UART_Init(void)
{
}
void UART_Put_Char(unsigned char dl)
{
TXREG = dl;
}
void UART_Put_String(unsigned char *dl)
{
{
while(PIR1bits.TXIF == 0); TXREG = *dl++;
}
}
char UART_Data_Ready()
{
return RCIF;
}
char UART_Read()
{
while (!RCIF); return RCREG;
}
void main(void)
{
TRISBbits.TRISB0 = TRISBbits.TRISB1 = TRISCbits.TRISC7 = 1; TRISBbits.TRISB2 = TRISBbits.TRISB3 = TRISCbits.TRISC6 = 0;
UART_Init();
while(1)
{
{
UART_Put_String("NHOM 4 VO DOI ");
delay_ms(500);
}
{
UART_Put_String("TU DONG HOA VA DIEU KHIEN ");
delay_ms(500);
}
{
kt = UART_Read();
}
{
PORTBbits.RB2 = 1;
PORTBbits.RB3 = 0;
}
{
PORTBbits.RB3 = 1;
PORTBbits.RB2 = 0;
}
}
return;
}
#define _XTAL_FREQ 20000000
#define PRESCALE 4
#define RS RD2 #define EN RD3 #define D4 RD4 #define D5 RD5 #define D6 RD6 #define D7 RD7
#include <xc.h> #include <pic.h> #include <stdio.h> #include <stdlib.h> #include <pic16f877a.h>
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF #pragma config CP = OFF
char s1[20],s2[20];
void Timer1_Init()
{
}
void Int_Init()
{
}
void main(void)
{
TRISD = 0x00; TRISB0 = 1;
TRISB1= TRISB2 = TRISB3 = 0; TRISA0 = TRISA1 = TRISA2 = 1;
PORTA = 0xFF;
PORTB = PORTD = 0x00;
Lcd_Init(); ADC_Init();
{
{
PORTBbits.RB3 = 1;
PORTBbits.RB2 = 1;
}
PORTBbits.RB3 = 1;
PORTBbits.RB2 = 0;
}
{
PORTBbits.RB3 = 0;
}
Lcd_Set_Cursor(1,1); Lcd_Write_String(s1);
sprintf(s2,"TOC DO:%4.0f v/p ",tocdo); Lcd_Set_Cursor(2,1); Lcd_Write_String(s2);
}
return;
}
void interrupt NGAT(void)
{
{
}
{
TMR1L=34286&0X00FF;
}
}
#define _XTAL_FREQ 20000000
//Khai báo thư viện #include <stdio.h> #include <stdlib.h> #include <pic.h> #include <pic16f877a.h> #include <xc.h>
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF #pragma config CP = OFF
void I2C_Master_Init(unsigned long clock)
{
SSPCON2 = 0x00;
}
void I2C_Master_Wait()
{
}
void I2C_Master_Start()
I2C_Master_Wait(); SEN = 1;
}
void I2C_Master_Stop()
{
I2C_Master_Wait(); PEN = 1;
}
void I2C_Master_Restart()
{
I2C_Master_Wait(); RSEN = 1;
}
void I2C_Master_Write(unsigned char data)
{
I2C_Master_Wait(); SSPBUF = data;
}
unsigned char I2C_Master_Read(unsigned char ack)
{
unsigned char temp; I2C_Master_Wait();
I2C_Master_Wait();
I2C_Master_Wait();
}
void main(void)
{
I2C_Master_Start();
delay_ms(200); I2C_Master_Restart();
I2C_Master_Stop();
delay_ms(200);
}
}
#define _XTAL_FREQ 20000000
//Khai báo thư viện #include <stdio.h> #include <stdlib.h> #include <pic.h> #include <pic16f877a.h> #include <xc.h>
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF #pragma config CP = OFF
unsigned char z;
void I2C_Slave_Init(unsigned char address)
{
void interrupt I2C_Slave_Read()
{
{
{
}
{
}
{
}
}
}
void main()
{
TRISD = 0x00;
}
CODE CHO MASTE
#define _XTAL_FREQ 20000000
//Khai báo thư viện #include <stdio.h> #include <stdlib.h> #include <pic.h> #include <pic16f877a.h> #include <xc.h>
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF
void SPI_Master_Init()
{
SSPCONbits.SSPM3 = 0;
SSPCONbits.SSPM2 = 0;
SSPCONbits.SSPM1 = 1;
}
unsigned char SPI_Master_Read()
{
}
void SPI_Master_Write(unsigned char Tvalue)
{
}
void main()
{
SPI_Master_Init();
TRISD = 0x00;
PORTB = 0xff;
while(1)
{
delay_ms(100);
delay_ms(100);
}
}
CODE CHO SLAVE
#define _XTAL_FREQ 20000000
//Khai báo thư viện #include <stdio.h> #include <stdlib.h> #include <pic.h> #include <pic16f877a.h> #include <xc.h>
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF
void SPI_Slave_Init()
{
SSPCONbits.SSPM2 = 1;
SSPCONbits.SSPM1 = 0;
}
unsigned char SPI_Slave_Read()
{
}
void SPI_Slave_Write(unsigned char Tvalue)
{
}
void main()
{
SPI_Slave_Init();
TRISD = 0x00;
PORTB = 0xFF;
while(1)
{
delay_ms(100);
delay_ms(100);
}
}
#define _XTAL_FREQ 20000000
#include <stdio.h> #include <stdlib.h> #include <pic.h> #include <pic16f877a.h> #include <xc.h>
// CONFIG #pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF
#define DATA PORTB
const unsigned char cot[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
const unsigned char hang[] = {
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0x03,0x01,0xf4,0xe4,0xe4,0x01,0x03,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
};
void vaodulieu(unsigned char m)
{
unsigned char t,temp;
{
temp = m&0x80; if(temp == 0x80) DS=1;
else DS =0;
SCK =1;
}
}
void main()
{
TRISD = 0x18;
while (1)
{
{
{
{
{
vaodulieu(hang[i+k+8]);
vaodulieu(hang[i+k]);
STR=1;
delay_ms(5); DATA=0x00;
}
}
}
}
{
delay_ms(50); while (LTR ==0);
for(i=24;i>0;i--)
{
for(j=0;j<10;j++)
{
for(k=0;k<8;k++)
{
vaodulieu(hang[i+k+8]); vaodulieu(hang[i+k]); STR=0;
STR=1;
DATA=cot[k];
delay_ms(5); DATA=0x00;
}
}
}
}
}
}
#define _XTAL_FREQ 20000000
#define D4 RB4 #define D5 RB5 #define D6 RB6 #define D7 RB7
#define C2 RD1 #define C3 RD2 #define C4 RD3 #define H1 RD4 #define H2 RD5 #define H3 RD6 #define H4 RD7
#include <stdio.h> #include <stdlib.h> #include <xc.h> #include "LCD16x2.h"
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = ON #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF
unsigned char Quetphim()
{
unsigned char giatri,i;
for(i=0;i<4;i++)
{
if(H1==0) { delay_ms(40);if(H1==0){ while(H1==0); giatri = 1 + 4*i;}}
if(H2==0) { delay_ms(40);if(H2==0){ while(H2==0); giatri = 2 + 4*i;}} //Vi du hang 2 cot 1(0xfe) ta thu dk gt=2
if(H3==0) { delay_ms(40);if(H3==0){ while(H3==0); giatri = 3 + 4*i;}}
if(H4==0) { delay_ms(40);if(H4==0){ while(H4==0); giatri = 4 + 4*i;}}
}
}
void main()
{
TRISB = 0x00; TRISD = 0xF0; PORTB = 0x00; PORTD = 0xF0;
unsigned char num,check,check1,KT,gt1,gt2,pt,a[16]; float kq;
const unsigned int Mangso[] =
const unsigned char Mang[] =
while(1)
{
{
{
}
}
{
if(num!=0&&num<11)
{
}
Lcd_Write_Char(Mang[num-1]);
{
}
{
}
{
}
}
{
{
check=1; Lcd_Write_Char(Mang[num-1]);
}
}
{
num=Mangso[Quetphim()]; if(num!=0&&num<11)
{
Lcd_Write_Char(Mang[num-1]);
if(num==10)
{
}
else
{
}
}
gt2=gt2*10;
gt2=(gt2*10)+num;
{
kq=(float)gt1/gt2;
}
{
kq=(float)gt1*gt2;
}
{
kq=(float)gt1 - gt2;
}
{
kq=(float)gt1+gt2;
}
}
}
{
sprintf(a,"%4.2f",kq); Lcd_Write_String(a);
}
{
}
}
}