?? demo.c
字號:
/*
LuckyProg Mega8 BootLoader 引導程序 V1.0
文件名 : demo.c
作 者: 芯 藝
更新時間: 2004-08-30
CPU : ATMEGA8
時 鐘: 4MHz
編 譯: WinAVR-20040720
E-mail : changfutong@sina.com
MSN : changfutong@hotmail.com
OICQ : 27796915
歡迎訪問: http://BitFu.yeah.net
注:本程序在硬件上調試通過,
與計算機程序LuckyProg Mega8 BootLoader(LuckyProg M8BL)V1.0配合使用。
*/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/boot.h>
#define uchar unsigned char
#define uint unsigned int
#define FREQ 4
#define UART_ACK 0XAA
#define PAGE_SIZE 64
uint g_wPageIndex=0; //
uchar g_aPageTemp[PAGE_SIZE];
void (*reset)(void)=0x0000;
void uart_putc(uchar c)
{
while( !(UCSRA & (1<<UDRE)) );
UDR=c;
}
uchar uart_getc(void)
{
while( !(UCSRA & (1<<RXC)) );
return UDR;
}
void WritePage(void)
{
uchar i;
// 接收當前頁面數據
for(i=0;i<PAGE_SIZE;i++)
g_aPageTemp[i]=uart_getc();
// 頁擦除操作
boot_page_erase(g_wPageIndex<<6);
while(boot_rww_busy())
boot_rww_enable();
// 填充緩沖頁
for(i = 0; i < PAGE_SIZE; i += 2)
boot_page_fill((unsigned long)i,*((uint *)(g_aPageTemp +i)));
// 頁寫入操作
boot_page_write(g_wPageIndex<<6);
while(boot_rww_busy())
boot_rww_enable();
g_wPageIndex++;
}
void ReadPage(void)
{
uchar i;
for(i=0;i<PAGE_SIZE;i++)
uart_putc(pgm_read_byte(i+(g_wPageIndex*PAGE_SIZE)));
g_wPageIndex++;
}
int main(void)
{
uchar tmp;
//uart 初始化
UBRRH=0;
UBRRL=25;//9600 baud 6MHz:38 4MHz:25
UCSRB=(1<<RXEN)|(1<<TXEN);
while(1)
{
tmp=uart_getc();//recv command
switch(tmp)
{
case 0xB0://設置頁地址
g_wPageIndex=uart_getc();
uart_putc(g_wPageIndex);
break;
case 0xBF://運行用戶程序
reset();
break;
case 0xAF://寫一頁
WritePage();
uart_putc(UART_ACK);//應答
break;
case 0xA0://讀一頁
ReadPage();
break;
case UART_ACK://回應檢測命令
uart_putc(UART_ACK);
break;
default:
break;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -