|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2005-4-12 22:03:55
|
显示全部楼层
刚才好象服务器故障,文件没来得及上传,现在上传
C文件---------------------------------------------------------
/*****************************************************
Chip type : ATmega16L
Program type : Application
Clock frequency : 4.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include<m16_led.h>
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// USART Receiver buffer
#define RX_BUFFER_SIZE 50
char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer[rx_wr_index]=data;
if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
};
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif
// USART Transmitter buffer
#define TX_BUFFER_SIZE 50
char tx_buffer[TX_BUFFER_SIZE];
#if TX_BUFFER_SIZE<256
unsigned char tx_wr_index,tx_rd_index,tx_counter;
#else
unsigned int tx_wr_index,tx_rd_index,tx_counter;
#endif
// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void)
{
if (tx_counter)
{
--tx_counter;
UDR=tx_buffer[tx_rd_index];
if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
};
}
#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
#asm("cli")
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
{
tx_buffer[tx_wr_index]=c;
if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
++tx_counter;
}
else
UDR=c;
#asm("sei")
}
#pragma used-
#endif
// Standard Input/Output functions
#include <stdio.h>
// Declare your global variables here
//
uchar data;
void main(void)
{
PORTA=0xFF; // LEDÊý¾ÝÏߣ¬¶¨ÒåΪÊä³ö
DDRA=0xFF;
delayms(100);
PORTC=0x00; // LEDѡͨÏߣ¬¶¨ÒåΪÊä³ö
DDRC=0xFF;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// USART initialization
// Communication Parameters: 8 Data, 2 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 19200
UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x8E;
UBRRH=0x00;
UBRRL=0x0C;
#asm("sei")
led_chack_init();
led_chack_init_move();
led_data_buff_init();
while (1)
{
if(rx_counter)
{
led_left_move();
data=getchar();
putchar(data);
//led_char_append(data);
led_data_make(data); //°Ñ´®¿ÚÊÕµ½µÄ×Ö·ûÒÔASCIIÂëÊä³öµ½LEDÉÏ
}
led_mix();//LEDÏÔʾÊý¾Ý
}
}
H文件------------------
/*****************************************************
MEGA M16µÄLED ÊýÂë¹ÜÏÔʾµÄ³õʼ»¯
*****************************************************/
#include <mega16.h>
#include <math.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define led_datas PORTA //Êý¾ÝÏß
#define led_control_ports PORTC //ѡͨÏß
uchar led_control_port[]={0b11111110,0b11111101,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F}; //¹²Òõ¼« ÊýÂë¹Üѡͨ
uchar led[]={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6,0xEE,0x3E,0x1A,0x7A,0x9E,0x8E,0x02,0x00}; //abcdefgh ¸ß--µÍλ
/*0,1,2,3,4,5,6,7,8,9,A,b,c,d,E,F,-,¿Õ*/
uchar led_data_buff[8];//¹²8¸öLEDÏÔʾÊý¾Ý£¬¸öλÊǵÚ0¸ö
extern void delayms(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<10;j++)
;
}
void led_data_buff_init(void)
{
uchar counter;
for(counter=0;counter<8;counter++)led_data_buff[counter]=16;//LEDÊý¾ÝÇå¿Õ£¬Ã»Óбʻ­
}
extern void led_one(char led_data,char led_port) //Ò»´ÎÏÔʾһ¸öÊý×Ö
{
led_control_ports=led_control_port[led_port];//ÊýÂë¹Üѡͨ
led_datas=led[led_data_buff[led_data]];//°Ñ×ÖÐÎË͵½¶Ë¿ÚÉÏ
delayms(100);
}
extern void led_mix(void) //Ò»´ÎÏÔʾ8¸öÊý×Ö£¨8¸öÊýÂë¹Ü£©
{
char counter,i;
for(i=0;i<10;i++)
for(counter=0;counter<=7;counter++)led_one(counter,counter);
}
void led_data_make(int data) //°ÑÊý¾Ýת»»³ÉLED×ÖÐζ¨ÒåÊý¾Ý²¢°ÑËü·Åµ½led_data_buffÖÐ
{
bit fushuflag=0;
uchar counter,x;
led_data_buff_init();
if(data<0)
{
fushuflag=1;
data=-data;
}
for(counter=0;counter<8;counter++) //8¸öÊýÂë¹Ü£¬
{
if((data<10)&&(counter<7))
{
led_data_buff[counter]=data;
if((fushuflag==1)&&(counter<7))led_data_buff[++counter+1]=16;//"-"¸ººÅ±Ê»®
counter=8;
}
x=data%10;//ÇóÓàÊý,µÚ1´ÎÇó¸öλÊý£¬µÚ¶þ´ÎÇóʮλÊý¡£¡£¡£¡£
data=data/10;
led_data_buff[counter+1]=x;
}
}
void led_left_move (void)//LEDÊý×Ö×óÒÆÒ»´Î
{
uchar counter,x;
x=led_data_buff[7];
for(counter=8;counter>0;counter--)led_data_buff[counter]=led_data_buff[counter-1];
led_data_buff[0]=x;
}
uchar led_asc_chg(uchar data) //ת»»ÎªLED[]×ÖÐÎÐòºÅ
{
if(30<=data<=39)data=data-30;
if(61<=data<=66)data=data-51;
if(41<=data<=46)data=data-31;
return data;
}
void led_char_append(uchar data) //°ÑÊý×Öת»»³ÉLED×ÖÐÎÐòºÅ²¢°ÑËü·Åµ½led_data_buffÖÐ
{
led_left_move ();
led_data_buff[0]=led_asc_chg(data);
}
extern void led_chack_init(void)
{
uchar counter,i;
for(counter=0;counter<8;counter++)led_data_buff[counter]=counter;
for(i=0;i<10;i++)
{
for(counter=0;counter<2;counter++)led_mix();
PORTC=0xff;
delayms(20000);
}
}
extern void led_chack_init_move(void) //LEDÊý×ÖÁ¬Ðø×óÒÆ
{
uchar counter,i;
for(i=0;i<8;i++)
{
for(counter=0;counter<=8;counter++)
{led_left_move();
for( ;counter<2;counter++)led_mix();
}
}
}
/*¶Ë¿ÚÉèÖÃΪÊä³ö
PORTA=0xFF;
DDRA=0xFF;
delayms(100);
PORTC=0x00;
DDRC=0xFF;
*/ |
|