Bài 4: giao tiếp IC ghi dịch 74HC595
IC 74hc595 giúp mở rộng chân cho VĐK, thường được dùng để quét led 7 thanh, led matrix, sau đây là code tham khảo sử dụng ic 74hc595 để quét led 7 thanh catot chung:
#include <p33fj32mc202.h>
// Select Internal FRC at POR
_FOSCSEL(FNOSC_FRC); //su dung tan so noi 7.37MHz
// Enable Clock Switching and Configure
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF);
#define clk _LATB0
#define din _LATB1
#define lat _LATB2 // khai bao chan xuat cho ic 74hc595
#define led1 _LATB3
#define led2 _LATB4
#define led3 _LATB5
#define led4 _LATB6 // khai báo chan dk led
unsigned char count_led = 0;
char number[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; // ma led cattoth chung
char led[4];
unsigned char i,Q;
void display();
//void readADC();
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void) // ngat timer1
{
/* Interrupt Service Routine code goes here */
IFS0bits.T1IF = 0; // Clear Timer1 Interrupt Flag
display();
}
int main()
{
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD = 41; // M = 43
CLKDIVbits.PLLPOST=0; // N2 = 2
CLKDIVbits.PLLPRE=0; // N1 = 2 // su dung bo nhan tan so
// Initiate Clock Switch to Internal FRC with PLL (NOSC = 0b001)
__builtin_write_OSCCONH(0x01);
__builtin_write_OSCCONL(0x01);
// Wait for Clock switch to occur
while (OSCCONbits.COSC!= 0b001);
// Wait for PLL to lock
while(OSCCONbits.LOCK!= 1) {};
/*****SETUP Timer1 *****/
T1CONbits.TON = 0; // Disable Timer
T1CONbits.TCS = 0; // Select internal instruction cycle clock
T1CONbits.TGATE = 0; // Disable Gated Timer mode
T1CONbits.TCKPS = 0b01; // Select 1:8 Prescaler: clock timer1 =5MHz
TMR1 = 0x00; // Clear timer register
PR1 = 2500; // Load the period value
IPC0bits.T1IP = 0x01; // Set Timer1 Interrupt Priority Level
IFS0bits.T1IF = 0; // Clear Timer1 Interrupt Flag
IEC0bits.T1IE = 1; // Enable Timer1 interrupt
T1CONbits.TON = 1; // Start Timer
TRISB = 0;
while(1)
{
led[0] = number[9];
led[1] = number[3];
led[2] = number[8];
led[3] = number[7];
}
return 0;
}
void display()
{
count_led ++ ;
if(count_led ==1)
{
led4 = 0;
Q=led[0];
for(i=0;i<8;i++)
{din = 0;
if(Q&0x80)
din = 1;
clk=0;clk=1;Q<<=1;}
lat=0; lat=1; //Chot hien thi du lieu
led1 = 1;led2=led3=led4=0;
}
if(count_led ==2)
{
led1=0;
Q=led[1];
for(i=0;i<8;i++)
{din = 0;
if(Q&0x80)
din = 1;clk=0;clk=1;Q<<=1;}
lat=0; lat=1; //Chot hien thi du lieu
led2 = 1;led1=led3=led4=0;
}
if(count_led ==3)
{
led2 = 0;
Q=led[2];
for(i=0;i<8;i++)
{din = 0;
if(Q&0x80)
din = 1;
clk=0;clk=1;Q<<=1;}
lat=0; lat=1; //Chot hien thi du lieu
led3 = 1;led2=led1=led4=0;
}
if(count_led ==4)
{
led3 = 0;
Q=led[3];
for(i=0;i<8;i++)
{din = 0;
if(Q&0x80)
din = 1;
clk=0;clk=1;Q<<=1;}
lat=0; lat=1; //Chot hien thi du lieu
led4 = 1;led2=led3=led1=0;
}
if(count_led >4) count_led = 0;
}
#include <p33fj32mc202.h>
// Select Internal FRC at POR
_FOSCSEL(FNOSC_FRC); //su dung tan so noi 7.37MHz
// Enable Clock Switching and Configure
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF);
#define clk _LATB0
#define din _LATB1
#define lat _LATB2 // khai bao chan xuat cho ic 74hc595
#define led1 _LATB3
#define led2 _LATB4
#define led3 _LATB5
#define led4 _LATB6 // khai báo chan dk led
unsigned char count_led = 0;
char number[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; // ma led cattoth chung
char led[4];
unsigned char i,Q;
void display();
//void readADC();
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void) // ngat timer1
{
/* Interrupt Service Routine code goes here */
IFS0bits.T1IF = 0; // Clear Timer1 Interrupt Flag
display();
}
int main()
{
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD = 41; // M = 43
CLKDIVbits.PLLPOST=0; // N2 = 2
CLKDIVbits.PLLPRE=0; // N1 = 2 // su dung bo nhan tan so
// Initiate Clock Switch to Internal FRC with PLL (NOSC = 0b001)
__builtin_write_OSCCONH(0x01);
__builtin_write_OSCCONL(0x01);
// Wait for Clock switch to occur
while (OSCCONbits.COSC!= 0b001);
// Wait for PLL to lock
while(OSCCONbits.LOCK!= 1) {};
/*****SETUP Timer1 *****/
T1CONbits.TON = 0; // Disable Timer
T1CONbits.TCS = 0; // Select internal instruction cycle clock
T1CONbits.TGATE = 0; // Disable Gated Timer mode
T1CONbits.TCKPS = 0b01; // Select 1:8 Prescaler: clock timer1 =5MHz
TMR1 = 0x00; // Clear timer register
PR1 = 2500; // Load the period value
IPC0bits.T1IP = 0x01; // Set Timer1 Interrupt Priority Level
IFS0bits.T1IF = 0; // Clear Timer1 Interrupt Flag
IEC0bits.T1IE = 1; // Enable Timer1 interrupt
T1CONbits.TON = 1; // Start Timer
TRISB = 0;
while(1)
{
led[0] = number[9];
led[1] = number[3];
led[2] = number[8];
led[3] = number[7];
}
return 0;
}
void display()
{
count_led ++ ;
if(count_led ==1)
{
led4 = 0;
Q=led[0];
for(i=0;i<8;i++)
{din = 0;
if(Q&0x80)
din = 1;
clk=0;clk=1;Q<<=1;}
lat=0; lat=1; //Chot hien thi du lieu
led1 = 1;led2=led3=led4=0;
}
if(count_led ==2)
{
led1=0;
Q=led[1];
for(i=0;i<8;i++)
{din = 0;
if(Q&0x80)
din = 1;clk=0;clk=1;Q<<=1;}
lat=0; lat=1; //Chot hien thi du lieu
led2 = 1;led1=led3=led4=0;
}
if(count_led ==3)
{
led2 = 0;
Q=led[2];
for(i=0;i<8;i++)
{din = 0;
if(Q&0x80)
din = 1;
clk=0;clk=1;Q<<=1;}
lat=0; lat=1; //Chot hien thi du lieu
led3 = 1;led2=led1=led4=0;
}
if(count_led ==4)
{
led3 = 0;
Q=led[3];
for(i=0;i<8;i++)
{din = 0;
if(Q&0x80)
din = 1;
clk=0;clk=1;Q<<=1;}
lat=0; lat=1; //Chot hien thi du lieu
led4 = 1;led2=led3=led1=0;
}
if(count_led >4) count_led = 0;
}
Nhận xét
Đăng nhận xét