数码管显示及键盘控制程序
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Ctrl_IO_bit(1,0);
Ctrl_IO_bit(3,0); //前进
Ctrl_IO_bit(2,1);
}
}
if(m==7)
*AT91C_PIOA_PDR=0x00000004;//PA2由外设控制 ,调速
*AT91C_PIOA_ASR=0x00000004;//PA2由外设A控制
*AT91C_PWMC_ENA=0x00000004;//通道2使能寄存器
volatile char temp=0xff;
char k,x,newkey,m;
long int i;
void Ctrl_IO_bit(unsigned char IO_bit,unsigned char level)
{
unsigned long int data=0x00000001;
{
*AT91C_PIOA_PER=0x02e0e00b;//PA1.PA2.PA3的I/O功能使能
*AT91C_PIOA_OER=0x02e0000b; //输出使能
*AT91C_PIOA_OWER=0x02e0000b; //输出写使能
{
Ctrl_IO_bit(0,0);
Ctrl_IO_bit(1,0);
Ctrl_IO_bit(2,1); //前进
Ctrl_IO_bit(3,0);
}
if(m==2)
{
Ctrl_IO_bit(2,1);
delay(5000000);
}
else
{
Ctrl_IO_bit(0,0); //前进
*AT91C_PIOA_PPUDR=0x00004000;//上拉禁用,14
*AT91C_PIOA_PDR=0x00100000; //外设使能寄存器
*AT91C_PIOA_BSR=0x00100000;//使能外设B
*AT91C_AIC_SMR=*AT91C_AIC_SMR|0x00000020; //负沿触发
*AT91C_AIC_IECR=1<<AT91C_ID_IRQ0;//系统使能irq0
AT91C_AIC_SVR[AT91C_ID_IRQ0]=(long int)*io_irq_handler; //中断入口地址
/**AT91C_PIOA_PER=0x02e0e00f;//pio使能寄存器
}
*AT91C_AIC_EOICR=1; // End of Interrupt
*AT91C_AIC_ICCR=0x4; //清除IPR中相应的中断,沿触发有效
delay(100000); //arm7处理速度很快,以致7289反应速度较慢,因此加延时
Ctrl_IO_bit(3,0); //前进
delay(7000000);
Ctrl_IO_bit(3,0); //前右转
Ctrl_IO_bit(0,0);
Ctrl_IO_bit(1,1);
*AT91C_PIOA_OWER=0x02e0000f; //输出写使能寄存器
*AT91C_PIOA_ODSR= 0x02000000; //蜂鸣器不响
*AT91C_PMC_PCER=(unsigned int)1<<AT91C_ID_PIOA; //允许IO口的时钟
*AT91C_PMC_PCER=1<<AT91C_ID_PWMC; //外设时钟使能
Ctrl_IO_bit(0,0); //右转
Ctrl_IO_bit(1,1);
Ctrl_IO_bit(2,1);
Ctrl_IO_bit(3,0);
}
if(m==4)
{
Ctrl_IO_bit(3,0); //前左转
Ctrl_IO_bit(1,0);
Ctrl_IO_bit(0,1);
Ctrl_IO_bit(2,1);
data=data<<IO_bit;
if(level)
{
*AT91C_PIOA_ODSR =(*AT91C_PIOA_ODSR)|data;
}
else
{
data=~data;
*AT91C_PIOA_ODSR =(*AT91C_PIOA_ODSR)&data;
}
else
{
Ctrl_IO_bit(3,0); //前左转
Ctrl_IO_bit(1,0);
Ctrl_IO_bit(0,1);
*AT91C_PWMC_CH2_CMR=0x00000001; //模式选择,左对齐,低电平启动,对MCK进行2分频作为选择时钟
*AT91C_PWMC_CH2_CPRDR=16384;//周期选择,CPRD=MCK/X,其中X为分频因子
*AT91C_PWMC_CH2_CDTYR=5000;//
Write(0x80,0);
Write(0x80,0);
Write(0x81,0);
Write(0x82,0);
Write(0x83,0);
while(1)
{
if(newkey)
{
if(m==1)
#include<AT91SAM7S256.h>
void Write(unsigned char com,unsigned char dta);//发送带数据的命令
char read_zlg7289(unsigned char command);
void send_byte(unsigned char out_byte);//发送8位数据
Ctrl_IO_bit(2,1);
}
Байду номын сангаас
if(m==6) //避障
{
if(!(*AT91C_PIOA_PDSR&(1<<14))) //101
{
}
}
void delay(unsigned long int x) //延时函数
{
while(--x) ;
}
void Write(unsigned char com,unsigned char dta)//发送带数据的命令
{
Ctrl_IO_bit(21,0); //cs=0
Ctrl_IO_bit(0,1); //左转
Ctrl_IO_bit(1,0);
Ctrl_IO_bit(2,1);
Ctrl_IO_bit(3,0);
}
if(m==5)
{
if(!(*AT91C_PIOA_PDSR&(1<<14))) //循迹
delay(1000);
send_byte(com);
send_byte(dta);
Ctrl_IO_bit(21,1); //cs=1;
}
void io_irq_handler(void) __irq //I/O中断函数
Ctrl_IO_bit(0,0);
Ctrl_IO_bit(1,0);
Ctrl_IO_bit(2,0); //后退
Ctrl_IO_bit(3,1);
}
if(m==3)
{
{
Ctrl_IO_bit(3,0); //前右转
Ctrl_IO_bit(0,0);
Ctrl_IO_bit(1,1);
Ctrl_IO_bit(2,1);
m=2;
Write(0x83,2);
break;
case 0x02:
m=3;
Write(0x83,3);
break;
case 0x03:
m=4;
Write(0x83,4);
Write(0x83,6);
break;
case 0x07:
m=7;
Write(0x83,7);
break;
default : break;
*AT91C_PIOA_ODR=0x0000e000; // 设置成输入 13.14,15
*AT91C_PIOA_OER=0x02e0000f; //输出使能寄存器
*AT91C_PIOA_OWER=0x02e0000f; //输出写使能寄存器
*AT91C_PIOA_ODSR= 0x02000000; //蜂鸣器不响 */
{
newkey=0x01;
temp=read_zlg7289(0x15);
switch(temp)
{
case 0x00:
m=1;
Write(0x83,1);
break;
case 0x01:
break;
case 0x04:
m=8;
Write(0x83,8);
break;
case 0x05:
m=5;
Write(0x83,5);
break;
case 0x06:
m=6;
}
int main()
{
//*AT91C_RSTC_RMR = 0xa5000801;
*AT91C_PIOA_PER=0x02e0e00f;//pio使能寄存器
*AT91C_PIOA_ODR=0x0000e000; // 设置成输入 13.14,15
*AT91C_PIOA_OER=0x02e0000f; //输出使能寄存器
Ctrl_IO_bit(3,0); //前进
Ctrl_IO_bit(2,1);
}
}
if(m==7)
*AT91C_PIOA_PDR=0x00000004;//PA2由外设控制 ,调速
*AT91C_PIOA_ASR=0x00000004;//PA2由外设A控制
*AT91C_PWMC_ENA=0x00000004;//通道2使能寄存器
volatile char temp=0xff;
char k,x,newkey,m;
long int i;
void Ctrl_IO_bit(unsigned char IO_bit,unsigned char level)
{
unsigned long int data=0x00000001;
{
*AT91C_PIOA_PER=0x02e0e00b;//PA1.PA2.PA3的I/O功能使能
*AT91C_PIOA_OER=0x02e0000b; //输出使能
*AT91C_PIOA_OWER=0x02e0000b; //输出写使能
{
Ctrl_IO_bit(0,0);
Ctrl_IO_bit(1,0);
Ctrl_IO_bit(2,1); //前进
Ctrl_IO_bit(3,0);
}
if(m==2)
{
Ctrl_IO_bit(2,1);
delay(5000000);
}
else
{
Ctrl_IO_bit(0,0); //前进
*AT91C_PIOA_PPUDR=0x00004000;//上拉禁用,14
*AT91C_PIOA_PDR=0x00100000; //外设使能寄存器
*AT91C_PIOA_BSR=0x00100000;//使能外设B
*AT91C_AIC_SMR=*AT91C_AIC_SMR|0x00000020; //负沿触发
*AT91C_AIC_IECR=1<<AT91C_ID_IRQ0;//系统使能irq0
AT91C_AIC_SVR[AT91C_ID_IRQ0]=(long int)*io_irq_handler; //中断入口地址
/**AT91C_PIOA_PER=0x02e0e00f;//pio使能寄存器
}
*AT91C_AIC_EOICR=1; // End of Interrupt
*AT91C_AIC_ICCR=0x4; //清除IPR中相应的中断,沿触发有效
delay(100000); //arm7处理速度很快,以致7289反应速度较慢,因此加延时
Ctrl_IO_bit(3,0); //前进
delay(7000000);
Ctrl_IO_bit(3,0); //前右转
Ctrl_IO_bit(0,0);
Ctrl_IO_bit(1,1);
*AT91C_PIOA_OWER=0x02e0000f; //输出写使能寄存器
*AT91C_PIOA_ODSR= 0x02000000; //蜂鸣器不响
*AT91C_PMC_PCER=(unsigned int)1<<AT91C_ID_PIOA; //允许IO口的时钟
*AT91C_PMC_PCER=1<<AT91C_ID_PWMC; //外设时钟使能
Ctrl_IO_bit(0,0); //右转
Ctrl_IO_bit(1,1);
Ctrl_IO_bit(2,1);
Ctrl_IO_bit(3,0);
}
if(m==4)
{
Ctrl_IO_bit(3,0); //前左转
Ctrl_IO_bit(1,0);
Ctrl_IO_bit(0,1);
Ctrl_IO_bit(2,1);
data=data<<IO_bit;
if(level)
{
*AT91C_PIOA_ODSR =(*AT91C_PIOA_ODSR)|data;
}
else
{
data=~data;
*AT91C_PIOA_ODSR =(*AT91C_PIOA_ODSR)&data;
}
else
{
Ctrl_IO_bit(3,0); //前左转
Ctrl_IO_bit(1,0);
Ctrl_IO_bit(0,1);
*AT91C_PWMC_CH2_CMR=0x00000001; //模式选择,左对齐,低电平启动,对MCK进行2分频作为选择时钟
*AT91C_PWMC_CH2_CPRDR=16384;//周期选择,CPRD=MCK/X,其中X为分频因子
*AT91C_PWMC_CH2_CDTYR=5000;//
Write(0x80,0);
Write(0x80,0);
Write(0x81,0);
Write(0x82,0);
Write(0x83,0);
while(1)
{
if(newkey)
{
if(m==1)
#include<AT91SAM7S256.h>
void Write(unsigned char com,unsigned char dta);//发送带数据的命令
char read_zlg7289(unsigned char command);
void send_byte(unsigned char out_byte);//发送8位数据
Ctrl_IO_bit(2,1);
}
Байду номын сангаас
if(m==6) //避障
{
if(!(*AT91C_PIOA_PDSR&(1<<14))) //101
{
}
}
void delay(unsigned long int x) //延时函数
{
while(--x) ;
}
void Write(unsigned char com,unsigned char dta)//发送带数据的命令
{
Ctrl_IO_bit(21,0); //cs=0
Ctrl_IO_bit(0,1); //左转
Ctrl_IO_bit(1,0);
Ctrl_IO_bit(2,1);
Ctrl_IO_bit(3,0);
}
if(m==5)
{
if(!(*AT91C_PIOA_PDSR&(1<<14))) //循迹
delay(1000);
send_byte(com);
send_byte(dta);
Ctrl_IO_bit(21,1); //cs=1;
}
void io_irq_handler(void) __irq //I/O中断函数
Ctrl_IO_bit(0,0);
Ctrl_IO_bit(1,0);
Ctrl_IO_bit(2,0); //后退
Ctrl_IO_bit(3,1);
}
if(m==3)
{
{
Ctrl_IO_bit(3,0); //前右转
Ctrl_IO_bit(0,0);
Ctrl_IO_bit(1,1);
Ctrl_IO_bit(2,1);
m=2;
Write(0x83,2);
break;
case 0x02:
m=3;
Write(0x83,3);
break;
case 0x03:
m=4;
Write(0x83,4);
Write(0x83,6);
break;
case 0x07:
m=7;
Write(0x83,7);
break;
default : break;
*AT91C_PIOA_ODR=0x0000e000; // 设置成输入 13.14,15
*AT91C_PIOA_OER=0x02e0000f; //输出使能寄存器
*AT91C_PIOA_OWER=0x02e0000f; //输出写使能寄存器
*AT91C_PIOA_ODSR= 0x02000000; //蜂鸣器不响 */
{
newkey=0x01;
temp=read_zlg7289(0x15);
switch(temp)
{
case 0x00:
m=1;
Write(0x83,1);
break;
case 0x01:
break;
case 0x04:
m=8;
Write(0x83,8);
break;
case 0x05:
m=5;
Write(0x83,5);
break;
case 0x06:
m=6;
}
int main()
{
//*AT91C_RSTC_RMR = 0xa5000801;
*AT91C_PIOA_PER=0x02e0e00f;//pio使能寄存器
*AT91C_PIOA_ODR=0x0000e000; // 设置成输入 13.14,15
*AT91C_PIOA_OER=0x02e0000f; //输出使能寄存器