从事音频硬件这么多年,但一直对单片机特别感兴趣,于是准备把多年的单片机设计编程再拾起来,做为兴趣,来学习。
于是自已找来AVR单片机,找来AVR DEMO,安装好AVR STUDIO, ICC V7 FOR AVR。从指令学起,数码管,LCD字符模块,先是用数码管写了一个简单的时钟程序,
#include<iom16v.h>
#include<macros.h>
#define uchar unsigned char
#define uint unsigned int
uchar num,miao,fen,shi,miaoge,miaoshi,fenge,fenshi,shige,shishi,a,b,c,d;
#pragma data:code
const table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0};
const tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(uint ms) //延时函数
{
uint i,j;
for (i=0;i<ms;i++)
{
for(j=0;j<1141;j++);
}
}
/***************初始化*************/
void init()
{
DDRA=0Xff;
DDRC=0xff;
DDRB=0xf8;
PORTA=0X00;
PORTC=0x00;
PORTB=0x07;
}
/**********定时器初始化***********/
void timer1_init(void)
{
TCCR1B=0x04;//256分频
TCNT1H=0x8f;
TCNT1L=0x80;
TIMSK|=BIT(2); //定时器中断使能位
SREG|=BIT(7); //设置总中断
}
/***************按键扫描**************/
void key_scan()
{
if(!(PINB&(1<<PB0)))
{
delay(10);
if(!(PINB&(1<<PB0)))
{
miao++;
if(miao=60)
{
miao=0;
}
while(!(PINB&(1<<PB0)))
{
display(miao,fen,shi);
}
}
}
if(!(PINB&(1<<PB1)))
{
delay(10);
if(!(PINB&(1<<PB1)))
{
fen++;
if(fen=60)
{
fen=0;
}
while(!(PINB&(1<<PB1)))
{
display(miao,fen,shi);
}
}
}
if(!(PINB&(1<<PB2)))
{
delay(10);
if(!(PINB&(1<<PB2)))
{
shi++;
if(shi=24)
{
shi=0;
}
while(!(PINB&(1<<PB2)))
{
display(miao,fen,shi);
}
}
}
}
/*************显示函数*************/
void display(uchar miao,uchar fen, uchar shi)
{
PORTA=table[miao%10];
PORTC=tab[7];
delay(1);
PORTA=0x00;
PORTA=table[miao/10];
PORTC=tab[6];
delay(1);
PORTA=0x00;
PORTA=0x40;
PORTC=tab[5];
delay(1);
PORTA=table[fen%10];
PORTC=tab[4];
delay(1);
PORTA=0x00;
PORTA=table[fen/10];
PORTC=tab[3];
delay(1);
PORTA=0x00;
PORTA=0x40;
PORTC=tab[2];
delay(1);
PORTA=0x00;
PORTA=table[shi%10];
PORTC=tab[1];
delay(1);
PORTA=0x00;
PORTA=table[shi/10];
PORTC=tab[0];
delay(1);
PORTA=0x00;
}
/*************主函数**************/
void main(void)
{
init(); //初始化
timer1_init();
while(1)
{
key_scan();
display(miao,fen,shi);
}
}
/************中断函数*************/
#pragma interrupt_handler timer1_isr:9
void timer_isr(void)
{
miao++;
if(miao=60)
{
miao=0;
fen++;
if(fen=60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
}
}
TCNT1H=0x8f;
TCNT1L=0x80;
}
接下来的调式,还是很有意思的
首先遇到的语法的错误的,
C:\iccv7avr\bin\imakew -f SHIJIAN.mak
iccavr -c -IC:\icc\include\ -e -D__ICC_VERSION=722 -DATtiny861 -l -g -MEnhanced -Wa-W shijian.c
!W C:\iccv7avr\examples.avr\视频教程\lesson4\shijian.c(23):[warning] old-style function definition for `init'
!W C:\iccv7avr\examples.avr\视频教程\lesson4\shijian.c(43):[warning] old-style function definition for `key_scan'
!W C:\iccv7avr\examples.avr\视频教程\lesson4\shijian.c(56):[warning] calling a function without prototype may cause runtime errors if the function
does not return int or unsigned int
!W C:\iccv7avr\examples.avr\视频教程\lesson4\shijian.c(72):[warning] calling a function without prototype may cause runtime errors if the function
does not return int or unsigned int
!W C:\iccv7avr\examples.avr\视频教程\lesson4\shijian.c(88):[warning] calling a function without prototype may cause runtime errors if the function
does not return int or unsigned int
!W C:\iccv7avr\examples.avr\视频教程\lesson4\shijian.c(95):[warning] declaration of `display' does not match previous declaration at C:\iccv7avr\examples.avr\视频教程\lesson4\shijian.c(56)
!E shijian.s(7): cannot initialize RAM area
C:\iccv7avr\bin\imakew.exe: Error code 1
C:\iccv7avr\bin\imakew.exe: 'shijian.o' removed.
Done: there are error(s). Exit code: 1.
这23行-43行,52行,要加入void.问题解决了,
Device 12% full.
Done.
接下就可以演示了。
挺有意思的,请大家一起来讨论。
用户1686526 2013-4-27 12:58
用户1689783 2013-4-15 18:35
xucun915_925777961 2013-3-18 15:40
用户1677438 2013-3-15 22:36
用户618388 2013-3-12 22:37
用户1673561 2013-3-4 11:36
gordon85_618138855 2013-2-27 08:32
用户837270 2013-2-18 22:34
用户1665654 2013-2-18 18:36
用户1602177 2013-2-3 21:01