搜索
bottom↓
回复: 3

我做了学习板的UART及数码管的实验,请多指教

[复制链接]

出0入0汤圆

发表于 2005-4-12 21:40:58 | 显示全部楼层 |阅读模式
我做了学习板的UART及数码管的实验,下面是我的源程序,利用串行调试程序发给UART字符,UART发回来,并且在LED上显示二进制码值,LED并且有左移、自检显示。CVAR编译成功。现在有两个问题,想向高手请教:

  1、HEX文件有3K多,感觉有点大,请高手帮我瘦瘦身。

  2、用串行调试程序发“3”,收回来的是“3”,二进制码为“33”,但在LED数码管上显示“51”,查ASCII码表“3”也为“51”,到底是怎么回事。

  3、注意:串口:码率:19200,8位数字,2位停止位。4M晶体。串口程序是自动生成的

一点心得:

  实验过程中不小心把溶丝位搞成外部时钟,ISP无法接通,后用实验板的固定频率发生器4060上的128K时钟接到X1(?)上,ISP立即接通,赶紧该溶丝位,好险。



必须关闭JTAG功能溶丝位
-----此内容被eric于2005-04-12,22:13:39编辑过

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

 楼主| 发表于 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&Ecirc;&yacute;&frac34;&Yacute;&Iuml;&szlig;&pound;&not;&para;¨&Ograve;&aring;&Icirc;&ordf;&Ecirc;&auml;&sup3;&ouml;

DDRA=0xFF;      

delayms(100);



PORTC=0x00;  // LED&Ntilde;&iexcl;&Iacute;¨&Iuml;&szlig;&pound;&not;&para;¨&Ograve;&aring;&Icirc;&ordf;&Ecirc;&auml;&sup3;&ouml;

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);  //°&Ntilde;&acute;&reg;&iquest;&Uacute;&Ecirc;&Otilde;&micro;&frac12;&micro;&Auml;×&Ouml;·&ucirc;&Ograve;&Ocirc;ASCII&Acirc;&euml;&Ecirc;&auml;&sup3;&ouml;&micro;&frac12;LED&Eacute;&Iuml;

       }

      

     led_mix();//LED&Iuml;&Ocirc;&Ecirc;&frac34;&Ecirc;&yacute;&frac34;&Yacute;

    }

}





H文件------------------

/*****************************************************

MEGA M16&micro;&Auml;LED &Ecirc;&yacute;&Acirc;&euml;&sup1;&Uuml;&Iuml;&Ocirc;&Ecirc;&frac34;&micro;&Auml;&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;

*****************************************************/



#include <mega16.h>

#include <math.h>

#include<stdio.h>

#define uchar      unsigned char

#define uint       unsigned int





#define led_datas PORTA     //&Ecirc;&yacute;&frac34;&Yacute;&Iuml;&szlig;

#define led_control_ports PORTC  //&Ntilde;&iexcl;&Iacute;¨&Iuml;&szlig;

uchar led_control_port[]={0b11111110,0b11111101,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};  //&sup1;&sup2;&Ograve;&otilde;&frac14;&laquo; &Ecirc;&yacute;&Acirc;&euml;&sup1;&Uuml;&Ntilde;&iexcl;&Iacute;¨

uchar led[]={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6,0xEE,0x3E,0x1A,0x7A,0x9E,0x8E,0x02,0x00};    //abcdefgh &cedil;&szlig;--&micro;&Iacute;&Icirc;&raquo;

/*0,1,2,3,4,5,6,7,8,9,A,b,c,d,E,F,-,&iquest;&Otilde;*/



uchar led_data_buff[8];//&sup1;&sup2;8&cedil;&ouml;LED&Iuml;&Ocirc;&Ecirc;&frac34;&Ecirc;&yacute;&frac34;&Yacute;&pound;&not;&cedil;&ouml;&Icirc;&raquo;&Ecirc;&Ccedil;&micro;&Uacute;0&cedil;&ouml;



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&Ecirc;&yacute;&frac34;&Yacute;&Ccedil;&aring;&iquest;&Otilde;&pound;&not;&Atilde;&raquo;&Oacute;&ETH;±&Ecirc;&raquo;&shy;

}





extern void led_one(char led_data,char led_port) //&Ograve;&raquo;&acute;&Icirc;&Iuml;&Ocirc;&Ecirc;&frac34;&Ograve;&raquo;&cedil;&ouml;&Ecirc;&yacute;×&Ouml;

{

led_control_ports=led_control_port[led_port];//&Ecirc;&yacute;&Acirc;&euml;&sup1;&Uuml;&Ntilde;&iexcl;&Iacute;¨

led_datas=led[led_data_buff[led_data]];//°&Ntilde;×&Ouml;&ETH;&Icirc;&Euml;&Iacute;&micro;&frac12;&para;&Euml;&iquest;&Uacute;&Eacute;&Iuml;

delayms(100);

}



extern void led_mix(void)  //&Ograve;&raquo;&acute;&Icirc;&Iuml;&Ocirc;&Ecirc;&frac34;8&cedil;&ouml;&Ecirc;&yacute;×&Ouml;&pound;¨8&cedil;&ouml;&Ecirc;&yacute;&Acirc;&euml;&sup1;&Uuml;&pound;&copy;

{

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)     //°&Ntilde;&Ecirc;&yacute;&frac34;&Yacute;×&ordf;&raquo;&raquo;&sup3;&Eacute;LED×&Ouml;&ETH;&Icirc;&para;¨&Ograve;&aring;&Ecirc;&yacute;&frac34;&Yacute;&sup2;&cent;°&Ntilde;&Euml;ü·&Aring;&micro;&frac12;led_data_buff&Ouml;&ETH;

{

bit fushuflag=0;

uchar counter,x;

led_data_buff_init();



if(data<0)

{

fushuflag=1;

data=-data;

}



for(counter=0;counter<8;counter++)   //8&cedil;&ouml;&Ecirc;&yacute;&Acirc;&euml;&sup1;&Uuml;&pound;&not;

{

if((data<10)&&(counter<7))

{

led_data_buff[counter]=data;

if((fushuflag==1)&&(counter<7))led_data_buff[++counter+1]=16;//"-"&cedil;&ordm;&ordm;&Aring;±&Ecirc;&raquo;&reg;

counter=8;

}

x=data%10;//&Ccedil;ó&Oacute;à&Ecirc;&yacute;,&micro;&Uacute;1&acute;&Icirc;&Ccedil;ó&cedil;&ouml;&Icirc;&raquo;&Ecirc;&yacute;&pound;&not;&micro;&Uacute;&para;&thorn;&acute;&Icirc;&Ccedil;ó&Ecirc;&reg;&Icirc;&raquo;&Ecirc;&yacute;&iexcl;&pound;&iexcl;&pound;&iexcl;&pound;&iexcl;&pound;

data=data/10;

led_data_buff[counter+1]=x;





}

}



void led_left_move (void)//LED&Ecirc;&yacute;×&Ouml;×ó&Ograve;&AElig;&Ograve;&raquo;&acute;&Icirc;

{

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)  //×&ordf;&raquo;&raquo;&Icirc;&ordf;LED[]×&Ouml;&ETH;&Icirc;&ETH;ò&ordm;&Aring;

{

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)     //°&Ntilde;&Ecirc;&yacute;×&Ouml;×&ordf;&raquo;&raquo;&sup3;&Eacute;LED×&Ouml;&ETH;&Icirc;&ETH;ò&ordm;&Aring;&sup2;&cent;°&Ntilde;&Euml;ü·&Aring;&micro;&frac12;led_data_buff&Ouml;&ETH;

{

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&Ecirc;&yacute;×&Ouml;&Aacute;&not;&ETH;&oslash;×ó&Ograve;&AElig;

{

uchar counter,i;

for(i=0;i<8;i++)

{

for(counter=0;counter<=8;counter++)

{led_left_move();

for( ;counter<2;counter++)led_mix();

}

}

}





         

/*&para;&Euml;&iquest;&Uacute;&Eacute;è&Ouml;&Atilde;&Icirc;&ordf;&Ecirc;&auml;&sup3;&ouml;



PORTA=0xFF;

DDRA=0xFF;      

delayms(100);





PORTC=0x00;

DDRC=0xFF;

*/

出0入0汤圆

发表于 2005-4-12 22:33:22 | 显示全部楼层
1. HEX文件的大小并不是真正程序的大小,有很多hex格式必须的信息。真正的大小,你可以看编译最后的结果提示。或者将此hex转换成bin文件看。



2. 将串口调试程序中“以十六进制发送”以及“以十六进制显示”前面的勾选上。

出0入0汤圆

 楼主| 发表于 2005-4-12 23:28:31 | 显示全部楼层
谢谢!
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-18 13:09

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表