任务一:当按下外部中断INT1/P33,P01的LED改变原来的状态
2.代码:
#include "io.h"
u8 State1=0; //LED1初始状态
u8 State2=0; //LED2初始状态
u8 State3=0; //LED3初始状态
u16 Key_Vol = 0;
u8Seg_no = 0;
u8 SEG_NUM[]=
{
0x3F, /*'0', 0*/
0x06, /*'1', 1*/
0x5B, /*'2', 2*/
0x4F, /*'3', 3*/
0x66, /*'4', 4*/
0x6D, /*'5', 5*/
0x7D, /*'6', 6*/
0x07, /*'7', 7*/
0x7F, /*'8', 8*/
0x6F, /*'9', 9*/
0x77, /*'A', 10*/
0x7C, /*'B', 11*/
0x39, /*'C', 12*/
0x5E, /*'D', 13*/
0x79, /*'E', 14*/
0x71, /*'F', 15*/
0x40, /*'-', 16*/
0x00, /*' ', 17*/
0x80, /*'.', 18*/
};
u8 T_NUM=
{
0X01,0X02,0X04,0X08,0X10,0X20,0X40,0X80
};
void LED0_Blink(void)
{
State1 = !State1;
P00 = State1;
}
void LED1_Blink(void)
{
State2 = !State2;
P01 = State2;
}
void LED2_Blink(void)
{
State3 = !State3;
P02 = State3;
}
void KEY_Task(void)
{
if(P33==0)
{
Key_Vol++;
if(Key_Vol==5)
{
// //按键按下的任务
// printf("按键单击\r\n");
USB_Reset_U();
IAP_CONTR = 0X60;
}
}
else
{
Key_Vol=0;
}
}
//#define ROW1P06
//#define ROW2 P07
//#define COL1 P00
//#define COL2 P01
//#define COL3 P02
//#define COL4 P03
//任务1:数码管显示当前的按键号
//按键识别原理:端口默认为高电平,实时读取到引脚为低电平是表示按下。
u8 Key_num = 0xff;
void Task_1(void)
{
//①第一步:现将P0.0-P0.3输出低电平,P0.6-P0.7输出高电平,如果有按键按下,按下的那一行的IO就会变成低电平,就可以判断出哪一行按下了。.
COL1 = 0;
COL2 = 0;
COL3 = 0;
COL4 = 0;
ROW1 = 1;
ROW2 = 1;
if((ROW1==0) || (ROW2==0)) //如果行按键有按下
{
if((ROW1==0)&&(ROW2==0)) //如果两行都有按键按下,不处理
{
}
else if(((ROW1==1)&&(ROW2==0)) || ((ROW1==0)&&(ROW2==1)))//如果有单颗按键按下
{
if(ROW1==0) //判断哪一行,输出行开始的序号
Key_num=0;
else if(ROW2==0)
Key_num=4;
COL1 = 1 ;
COL2 = 1;
COL3 = 1;
COL4 = 1;
ROW1 = 0;
ROW2 = 0;
if(COL1 == 0) //判断哪一列,叠加按键的序号
{
// Key_num = Key_num;
}
else if(COL2 == 0)
{
Key_num = Key_num+1;
}
else if(COL3 == 0)
{
Key_num = Key_num+2;
}
else if(COL4 == 0)
{
Key_num = Key_num+3;
}
}
COL1 = 0;
COL2 = 0;
COL3 = 0;
COL4 = 0;
ROW1 = 1;
ROW2 = 1;
}
else
{
Key_num = 0xff;
}
}
void Init_595(void)
{
HC595_SER =0;
HC595_RCK =0;
HC595_SCK =0;
}
void Send_595(u8 dat)
{
u8 i;
for(i=0;i<8;i++)
{
dat<<=1; //DAT=(DAT<<1); //CY
HC595_SER= CY; //先把数据写到引脚上
HC595_SCK = 1; //输出上升沿的时钟信号
HC595_SCK = 0;
}
}
void Display_Seg(u8 HC595_1,u8 HC595_2)
{
Send_595(HC595_1); //数码管段码输出高电平点亮
Send_595(HC595_2); //数码管位码输出低电平点亮
HC595_RCK = 1; //数据输出
HC595_RCK = 0;
}
//void SEG_Task(void)
//{
// if(Key_num==255)
// Display_Seg(SEG_NUM,~T_NUM); //数码管刷新段码和位码
// else
// Display_Seg(SEG_NUM,~T_NUM); //数码管刷新段码和位码
//}
u8 password={16,16,16,16,16,16,16,16};
void SEG_Task(void)
{
u8 num=0;
if(Seg_no==0)
{
Display_Seg(SEG_NUM],~T_NUM);
}
else if(Seg_no==1)
{
Display_Seg(SEG_NUM],~T_NUM);
}
else if(Seg_no==2)
{
Display_Seg(SEG_NUM],~T_NUM);
}
else if(Seg_no==3)
{
Display_Seg(SEG_NUM],~T_NUM);
}
else if(Seg_no==4)
{
Display_Seg(SEG_NUM],~T_NUM);
}
else if(Seg_no==5)
{
Display_Seg(SEG_NUM],~T_NUM);
}
else if(Seg_no==6)
{
Display_Seg(SEG_NUM],~T_NUM);
}
else if(Seg_no==7)
{
Display_Seg(SEG_NUM],~T_NUM);
}
else
{
}
Seg_no ++;
if(Seg_no>7)
Seg_no=0;
}
u8Key_Vol3 = 0;
u8Key_no = 0;
void PV_write_Task(void)
{
if(Key_num<0xff)
{
Key_Vol3++;
if(Key_Vol3==5)
{
if(Key_no==0)
{
password=16;
password=16;
password=16;
password=16;
password=16;
password=16;
password=16;
password=16;
}
password=Key_num;
Key_no++;
//password=17;
if(Key_no==8) //密码输入到了八位
{
if((password==1)&&(password==2)&&(password==3)&&(password==4)&&(password==5)&&(password==6)&&(password==7)&&(password==0))
{
password=17;
password=17;
password=17;
password=17;
password=17;
password=17;
password=17;
password=1;
}
else
{
password=16;
password=16;
password=16;
password=16;
password=16;
password=16;
password=16;
password=16;
}
Key_no=0;
}
}
}
else
{
Key_Vol3=0;
}
}
void INT1_Init(void)
{
IT1 = 1; //下降沿中断
EX1 = 1; //打开中断允许
// EA = 1; //打开总中断
}
void INT_ISR(void) interrupt 2
{
P01 =!P01;
}
第14集已学习,已手敲代码,测试通过
任务1:IO中断程序,实现LED灯的亮灭
代码
void P3_IO_Init(void)
{
P3IM0 = 0X00;//io中断模式设置为下降沿
P3IM1 = 0X00;
P3INTE = 0X08; //打开中断
}
void P3_IO_ISR(void)interrupt 40
{
u8 intf;
intf = P3INTF;
if(intf) //判断有没有io触发中断
{
P3INTF=0;
if(intf & 0x08)//判断是否是P33按下
{
P01 =!P01;
}
}
}
任务2:实现按下P33口显示数字1,按下P47口显示数字2
代码
void P3_IO_Init(void)
{
P3IM0 = 0X00;//io中断模式设置为下降沿
P3IM1 = 0Xff;
P3INTE = 0X08; //打开中断
}
void P3_IO_ISR(void)interrupt 40
{
u8 intf;
intf = P3INTF;
if(intf) //判断有没有io触发中断
{
P3INTF=0;
if(intf & 0x08)//判断是否是P33按下
{
password = 1;
//P01 =!P01;
}
}
}
void P4_IO_Init(void)
{
P4IM0 = 0X00;//io中断模式设置为下降沿
P4IM1 = 0Xff;
P4INTE = 0X80; //打开中断
}
void P4_IO_ISR(void)interrupt 41
{
u8 intf;
intf = P4INTF;
if(intf) //判断有没有io触发中断
{
P3INTF=0;
if(intf & 0x80)//判断是否是P33按下
{
password = 2;
//P01 =!P01;
}
}
}
任务3:实现P33,P47口的I0中断 ,P47口中断优先P33口中断
代码
void P4_IO_Init(void)
{
P4IM0 = 0X00;//io中断模式设置为下降沿
P4IM1 = 0Xff;
P4INTE = 0X80; //打开中断
PINIPH|= (1<<4); //写入最高优先级
PINIPL|= (1<<4);
}
void P4_IO_ISR(void)interrupt 41
{
u8 intf;
intf = P4INTF;
if(intf) //判断有没有io触发中断
{
P3INTF=0;
if(intf & 0x80)//判断是否是P33按下
{
password = 2;
//P01 =!P01;
}
}
}
第15集已学习,已手敲代码,测试通过
任务一:编写定时器1计数的程序
代码:#include "time.h"
u32 Count_T1 = 0;
void TIM1_Count_Init(void)
{
T1_CT = 1;//设置为外部计数
T1_M1 = 0;
T1_M0 = 0;
T1_GATE = 0;
TH1 = (65536-Count_num)>>8;
TL1 = (65536-Count_num);
P3PU |= 0x20;
TR1 = 1;
ET1 = 1;
}
void Timer1_Isr(void) interrupt 3
{
Count_T1 ++;
}
void T1_RunTask(void)
{
u32 count_th_tl = 0;
count_th_tl = ((u16)TH1 << 8)+(u16)TL1;
count_th_tl -=65526;
SEG7_ShowLong(Count_T1*Count_num+count_th_tl,10);
}
任务二:编写INT1测量低压电平时间
代码
void Timer1_Isr(void) interrupt 3
{
static u32 count_P33 = 0;
if( P33 == 0)
{
count_P33 ++;
}
else
{
if(count_P33>0)
{
Count_T1 = count_P33;
}
count_P33 = 0;
}
}
void Timer1_Init(void) //100微秒@24.000MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x38; //设置定时初始值
TH1 = 0xFF; //设置定时初始值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1; //使能定时器1中断
}
void T1_RunTask(void)
{
//SEG7_ShowLong(Count_T1,10);
SEG7_ShowString("%07.01f",((float)Count_T1)/10);
}
第十六集 串口的简单应用,测试通过
usart.h 代码
#include "usart.h"
#include "io.h"
bit B_TX2_Busy=0;
u8 Rec_Dat;
u8 Rec_Num = 0;
void Uart2_Isr(void) interrupt 8
{
if (S2CON & 0x02) //检测串口2发送中断
{
S2CON &= ~0x02;
B_TX2_Busy = 0; //清除串口2发送中断请求位
}
if (S2CON & 0x01) //检测串口2接收中断
{
S2CON &= ~0x01; //清除串口2接收中断请求位
Rec_Dat = S2BUF;
}
}
void Uart2_Init(void) //9600bps@24.000MHz
{
P_SW2 |= 0x01; //UART2/USART2: RxD2(P4.6), TxD2(P4.7)
S2CON = 0x50; //8位数据,可变波特率
AUXR |= 0x04; //定时器时钟1T模式
T2L = 0x8F; //设置定时初始值
T2H = 0xFD; //设置定时初始值
AUXR |= 0x10; //定时器2开始计时
IE2 |= 0x01; //使能串口2中断
Rec_Num = 0;
B_TX2_Busy = 0;
}
void Uart2_SendStr(u8 *puts) //串口数据发送函数
{
for(; *puts !=0;puts++) //遇到停止符0结束
{
S2BUF = *puts;
B_TX2_Busy=1;
while(B_TX2_Busy);
}
}
void Usart2_RunTask(void)
{
if(Rec_Num >= 6)
{
if((Rec_Dat =='\n')&&(Rec_Dat =='\r'))
{
if((Rec_Dat =='O')&&(Rec_Dat =='P')&&(Rec_Dat =='E')&&(Rec_Dat =='N'))
{
password=16;
password=16;
password=16;
password=16;
Uart2_SendStr("打开成功!\r\n");
}
else if((Rec_Dat =='C')&&(Rec_Dat =='L')&&(Rec_Dat =='O')&&(Rec_Dat =='S')&&(Rec_Dat =='E'))
{
password=17;
password=17;
password=17;
password=17;
Uart2_SendStr("关闭成功!\r\n");
}
Rec_Num = 0;
}
}
}
页:
1
[2]