[Atmel] 用AtmelStudio6.2跑mega16例程(51)Flash

[复制链接]
954|0
 楼主| ddllxxrr 发表于 2014-11-12 00:01 | 显示全部楼层 |阅读模式
本帖最后由 ddllxxrr 于 2014-11-12 00:23 编辑

本程序通过一个按键把FLASH里的数组通过串口打印出来。
FLASH.JPG

程序清单:
  1. /*
  2. * GccApplication14.c
  3. *
  4. * Created: 2014-11-11 18:26:34
  5. *  Author: Administrator
  6. */

  7. #define F_CPU 4000000UL
  8. #include <avr/pgmspace.h>
  9. #include <stdio.h>
  10. #include <avr/io.h>
  11. #include <stdint.h>

  12. typedef unsigned char PROGMEM prog_uchar;


  13. #define  K1_DOWN() (PINB & _BV(PB0)) == 0x00

  14. prog_int8_t Flash_Byte_Array[] = {0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,
  15.                                   0xFC,0xFD,0xFE,0xFF};
  16.                                                                   
  17. prog_int16_t Flash_Word_Array[] =
  18. {
  19.    0x00AC,0x0198,0x0233,0x03BC,0x0480,0x0598,0x06BE,0x07F8,0x0899,0x09A0};
  20.    
  21. void Init_USART()
  22. {
  23.     UCSRB=_BV(TXEN); //允许发送
  24.         UCSRC=_BV(URSEL)|_BV(UCSZ1)|_BV(UCSZ0);
  25.         UBRRL = (F_CPU/9600/16-1)%256;
  26.         UBRRH = (F_CPU/9600/16-1)/256;
  27. }

  28. void PutChar(char c)
  29. {
  30.     if(c=='\n') PutChar('\r');
  31.         UDR = c;
  32.         while(!(UCSRA & _BV(UDRE)));
  33. }

  34. void PutStr(char *s)
  35. {
  36.         while(*s) PutChar(*s ++);
  37. }


  38. int main(void)
  39. {
  40.     uint8_t Mem_byte;
  41.         uint16_t Mem_word,i,j = 0;
  42.         char s[6];
  43.         Init_USART();
  44.         PutStr("\n\n Press K1 to Start Read Data From Program Flash Memory...");
  45.         DDRB = 0x00; PORTB = 0xFF;
  46.         DDRD = 0xFF;
  47.         while(1)
  48.         {
  49.                 if(K1_DOWN())
  50.                 {
  51.                         PutStr("\n AVR Program Memory Data Demo........\n");
  52.                         PutStr("\n ---------------------------------\n");
  53.                         for(i=0,j=0;i<sizeof(Flash_Byte_Array);i++)
  54.                         {
  55.                                 Mem_byte = pgm_read_byte(&Flash_Byte_Array[i]);
  56.                                 sprintf(s,"%02X",Mem_byte); PutStr(s);
  57.                                 if(++j==20)
  58.                                 {
  59.                                        
  60.                                         j=0;PutStr("\n");
  61.                                 }
  62.                         }
  63.                         PutStr("\n_______________________________________\n ");
  64.                         for(i=0,j=0;i<sizeof(Flash_Word_Array);i++)
  65.                         {
  66.                                 Mem_word = pgm_read_word(&Flash_Word_Array[i]);
  67.                                 sprintf(s,"%04X",Mem_word); PutStr(s);
  68.                                 if(++j == 10)
  69.                                 {
  70.                                         j= 0; PutStr("\n");
  71.                                 }
  72.                         }
  73.                         
  74.                 }
  75.         }
  76.         
  77. }


您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2405

主题

7039

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部