#include <delay.h>
#define uchar unsigned char
#define uint unsigned int
#define zlg7289_cs PORTC.3
#define zlg7289_clk PORTC.4
#define zlg7289_dat PORTC.5
#define zlg7289_reset 0xa4 //Zlgzlg7289 Reset Control Data
#define zlg7289_rdkey 0x15 //Zlgzlg7289 Get Key control Data
uchar disdata[4];
uint a=1500;
int b=100;
/**********************************
函数:zlg7289_writebyte()
功能:向zlg7289写入1个字节的数据
参数:
dat:要写入的数据
**********************************/
void zlg7289_writebyte(char dat)
{
uchar i;
zlg7289_cs=0;
zlg7289_clk=0;
delay_us(50);
for(i=0;i<8;i++)
{
zlg7289_dat=(dat&0x80)?1:0;
//zlg7289_dat=(bit)(dat&0x80);
zlg7289_clk=1;
delay_us(50);
zlg7289_clk=0;
dat<<=1;
}
zlg7289_cs=1;
}
/**************************************
函数:zlg7289_write2bytes()
功能:向zlg7289总线写入2个字节的数据
返回:
***************************************/
void zlg7289_write2bytes(uchar addr,uchar dat)
{
uchar i;
zlg7289_cs=0;
delay_us(50);
for(i=0;i<8;i++)
{
zlg7289_dat=(addr&0x80)?1:0;
//zlg7289_dat=(bit)(addr&0x80);
zlg7289_clk=1;
delay_us(50);
zlg7289_clk=0;
addr<<=1;
}
delay_us(1);
for(i=0;i<8;i++)
{
zlg7289_dat=(dat&0x80)?1:0;
//zlg7289_dat=(bit)(dat&0x80);
zlg7289_clk=1;
delay_us(50);
zlg7289_clk=0;
dat<<=1;
}
zlg7289_cs=1;
delay_ms(10);
}
/**************************************
函数:zlg7289_init()
功能:初始化zlg7289芯片
返回:
***************************************/
void zlg7289_init()
{
zlg7289_cs=1;
zlg7289_clk=0;
zlg7289_writebyte(zlg7289_reset);
}
/**************************************
函数:display()
功能:显示一个整型量
返回:无
***************************************/
void display(uint t1)
{
disdata[0]=t1%10;
disdata[1]=t1%100/10;
disdata[2]=t1%1000/100;
disdata[3]=t1/1000;
zlg7289_write2bytes(0xc8,disdata[0]);
zlg7289_write2bytes(0xc9,disdata[1]);
zlg7289_write2bytes(0xca,disdata[2]);
zlg7289_write2bytes(0xcb,disdata[3]);
}
// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
// Place your code here
}
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0x00;
// Place your code here
}
// Declare your global variables here
void main(void)
{
PORTB=0x00;
DDRB=0xFF;
PORTC=0x00;
DDRC=0x7F;
PORTD=0x00;
DDRD=0xF7;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 31.250 kHz
TCCR0=0x04;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: Fast PWM top=ICR1
// OC1A output: Non-Inv.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x82;
TCCR1B=0x1A;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x4E;
ICR1L=0x20;
OCR1AH=0x05;
OCR1AL=0xDC;
OCR1BH=0x00;
OCR1BL=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: On
// INT1 Mode: Falling Edge
GICR|=0x80;
MCUCR=0x08;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
#asm("sei")
zlg7289_init();
while (1)
{
a=a+b;
OCR1A=a;
display(a);
if(a==2500)
b=-100;
if(a==700)
b=100;
delay_ms(500);
}
}
联系客服