又有几天没来这里了,还是老样子,最近终于开始写BHATMEGA-R2的程序了,不过还没有写完,这个板子的调试可能需要不少时间。另外,我计划着的那块PIC的开发板,这个星期也弄出来了,PCB以及程序都搞好,这两天送出去做,那个简易编程器我上次没搞定后就没再继续,有空还要继续调试,虽然这个东西现在没结果,不过我还是通过它把单片机的编程器原理搞清楚了,也是有收获的。这个年底我估计是完成不了手头上的事情的,看起来很忙,但是其实全是我自己做着玩的事情,并没有学校的正经项目做。
半个月前做好的start51学习板,虽然功能简单,不过对那些初学51单片机的学生以及社会上的爱好者还是很有用的,而且也比较便宜。呵呵。
下面把这个板子配套的hd7279程序贴出来,hd7279是驱动键盘和led(或者7段数码管)的芯片。资料google去找。声明下:这个程序是我用来测试start51板子的,在start51上运行通过,不过我不保证它没有bug,因此它只是个参考,如果你使用它在你的项目中,请你仍然仔细测试,本人不为你使用该程序带来的可能的损失付任何责任。
// FileName:hd7279.h
/***************************************
** Start51 study board test software
** The program test hd7279
** author:bluehacker<kernelpaniccrash at gmail.com>
** QQ:282074921
** Date:2007-11-16
******************************************/
#ifndef _LZP_HD7279A_H_
#define _LZP_HD7279A_H_
#include "reg52.h"
//#include <at89x52.h>
sbit HD7279A_CS=P1^3;
sbit HD7279A_CLK=P1^2;
sbit HD7279A_DATA=P1^1;
sbit HD7279A_KEY=P1^0;
//command define
#define HD7279_RESET 0xa4
#define HD7279_TEST 0xbf
#define HD7279_RLC 0xa3
#define HD7279_RRC 0xa2
#define HD7279_RL 0xa1
#define HD7279_RR 0xa0
#define HD7279_DECODE0 0x80
#define HD7279_DECODE1 0xc8
#define HD7279_UNDECODE 0x90
#define HD7279_HIDE 0x98
#define HD7279_FLASH 0x88
#define HD7279_SEGON 0xe0
#define HD7279_SEGOFF 0xc0
#define HD7279_READ 0x15
//设置波特率
#define OSC_FREQ 11059200L
#define BAUD_115200 256 - (OSC_FREQ/192L)/115200L // 255
#define BAUD_57600 256 - (OSC_FREQ/192L)/57600L // 254
#define BAUD_38400 256 - (OSC_FREQ/192L)/38400L // 253
#define BAUD_28800 256 - (OSC_FREQ/192L)/28800L // 252
#define BAUD_19200 256 - (OSC_FREQ/192L)/19200L // 250
#define BAUD_14400 256 - (OSC_FREQ/192L)/14400L // 248
#define BAUD_9600 256 - (OSC_FREQ/192L)/9600L // 244
void Delay_ms(unsigned int nMs);
void Delay_us(unsigned char nUs);
void send_byte(unsigned char c);
unsigned char recv_byte(void);
void write_hd7279(unsigned char cmd, unsigned char dat);
unsigned char read_hd7279(unsigned char cmd);
#endif
//FileName: HD7279.c
/***************************************
** Start51 study board test software
** The program test hd7279
** author:bluehacker<kernelpaniccrash at gmail.com>
** QQ:282074921
** Date:2007-11-16
******************************************/
#include "hd7279.h"
void send_byte(unsigned char c)
{
unsigned char i;
HD7279A_CS=0;
Delay_us(50);
for(i=0;i<8;i++)
{
if(c&0x80)
HD7279A_DATA=1;
else
HD7279A_DATA=0;
HD7279A_CLK=1;
Delay_us(10);
HD7279A_CLK=0;
Delay_us(10);
c<<=1;
}
HD7279A_DATA=0;
}
unsigned char recv_byte(void)
{
unsigned char ret="0";
unsigned char i;
HD7279A_DATA=1;
Delay_us(50);
for(i=0;i<8;i++)
{
HD7279A_CLK=1;
Delay_us(10);
ret<<=1;
if(HD7279A_DATA)
ret|=0x01;
HD7279A_CLK=0;
Delay_us(10);
}
HD7279A_DATA=0;
return ret;
}
void write_hd7279(unsigned char cmd, unsigned char dat)
{
send_byte(cmd);
send_byte(dat);
}
unsigned char read_hd7279(unsigned char cmd)
{
send_byte(cmd);
return recv_byte();
}
void main()
{
unsigned int cnt="0";
unsigned char key_num;
unsigned char digit[6];
unsigned char i,j,k;
unsigned long wait_cnt;
//初始化串口
TCON=0;
SCON=0x50;/*mode 1,1 start bit ,8 data bit ,1 stop bit,enable receive*/
PCON=0x80;/*SMOD=1, Baud Rate twice*/
TMOD=0x21;/*timer 0--mode 1 and timer 1 ---mode2*/
//set baud rate,use timer 1 as baud rate generator
TH1=BAUD_19200;
TL1=TH1;
TR1=1;
// ET1=0;//disable timer 1 interrupt
EA=0;
while(1)
{
Delay_ms(1000);
send_byte(HD7279_RESET);
Delay_ms(100);
//测试指令演示
send_byte(HD7279_TEST);
Delay_ms(5000);
send_byte(HD7279_RESET);
//闪烁指令及键盘接口演示
//将用户按键的键码显示出来,如果10秒内无按键或者按S0键则进入下一个演示项目
wait_cnt=0;
key_num=0xff;
write_hd7279(HD7279_FLASH,0xcf);//第5,6位设为闪烁显示
write_hd7279(HD7279_UNDECODE+4,0x08);//在第5位显示下划线
write_hd7279(HD7279_UNDECODE+5,0x08);//在第6位显示下划线
do
{
if(!HD7279A_KEY)//有键按下
{
key_num=read_hd7279(HD7279_READ);//读出键码
write_hd7279(HD7279_DECODE1+4,key_num/16);
write_hd7279(HD7279_DECODE1+5,key_num&0x0f);
while(!HD7279A_KEY);
wait_cnt=0;
}
wait_cnt++;
}while(wait_cnt<0x30000&&key_num!=7);
write_hd7279(HD7279_FLASH,0xff);//清除闪烁设置
//快速计数演示
for(i=0;i<6;i++)
{
digit=0;
write_hd7279(HD7279_DECODE0+i,digit);
}
while(digit[5]<2&&HD7279A_KEY)//计数到200000停止
{
digit[0]++;
if(digit[0]>9)
{
digit[0]=0;
digit[1]++;
if(digit[1]>9)
{
digit[1]=0;
digit[2]++;
if(digit[2]>9)
{
digit[2]=0;
digit[3]++;
if(digit[3]>9)
{
digit[3]=0;
digit[4]++;
if(digit[4]>9)
{
digit[4]=0;
digit[5]++;
}
}
}
}
}
write_hd7279(HD7279_DECODE0+5,digit[0]);
if(digit[0]==0)
{
write_hd7279(HD7279_DECODE0+4,digit[1]);
if(digit[1]==0)
{
write_hd7279(HD7279_DECODE0+3,digit[2]);
if(digit[2]==0)
{
write_hd7279(HD7279_DECODE0+2,digit[3]);
if(digit[3]==0)
{
write_hd7279(HD7279_DECODE0+1,digit[4]);
if(digit[4]==0)
{
write_hd7279(HD7279_DECODE0,digit[5]);
}
}
}
}
}
}
Delay_ms(5000);
send_byte(HD7279_RESET);//清除显示
//下载数据但不译码指令测试
//在数码管1上显示"三"
write_hd7279(HD7279_UNDECODE,0x49);
Delay_ms(5000);
//循环右移动测试,因为实验板上6个数码管的排列是这样的:
//数码管1-数码管2-...-数码管6
//则"三"从1一直移到6,再回到1
for(i=0;i<23;i++)
{
send_byte(HD7279_RRC);
Delay_ms(1000);
}
//再做左循环移动测试
for(i=0;i<23;i++)
{
send_byte(HD7279_RLC);
Delay_ms(1000);
}
//译码方式0,并右移测试
SBUF=0x80;
while(!TI);
TI=0;
send_byte(HD7279_RESET);
for(i=0;i<12;i++)
{
write_hd7279(HD7279_DECODE0+5,i);
Delay_ms(1000);
send_byte(HD7279_RR);
}
Delay_ms(5000);
send_byte(HD7279_RESET);
//译码方式1,并左移测试
SBUF=0x90;
while(!TI);
TI=0;
for(i=0;i<12;i++)
{
write_hd7279(HD7279_DECODE1,i);
Delay_ms(1000);
send_byte(HD7279_RL);
}
Delay_ms(5000);
//消隐演示
SBUF=0x10;
while(!TI);
TI=0;
j=0x3f;
k=1;
for(i=0;i<6;i++)
{
j=j&(~(k<<i));
write_hd7279(HD7279_HIDE,j);
Delay_ms(1000);
}
write_hd7279(HD7279_HIDE,0x3f);//恢复显示
Delay_ms(5000);
send_byte(HD7279_RESET);
//测试段亮,段灭
SBUF=0x20;
while(!TI);
TI=0;
for(i=0;i<48;i++)//将64个段逐个点亮,同时灭前一个段
{
write_hd7279(HD7279_SEGON,i);
if(i)
write_hd7279(HD7279_SEGOFF,i-1);
Delay_ms(1000);
}
Delay_ms(5000);
}
}
文章评论(0条评论)
登录后参与讨论