📄 mega32progman.c
字号:
#include <Mega32.h>
#include <delay.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "mmc.c"
void initialize(void); //all the usual mcu stuff
//RXC ISR variables
unsigned char r_index; //current string index
unsigned char r_buffer[16]; //input string
unsigned char r_ready; //flag for receive done
unsigned char r_char; //current character
unsigned char r_count; // wait until r_count chars received
unsigned char buffer[512]; // read in 512 bytes (256 instructions) at a time
void gets_int(int count);
void main(void)
{
register int i, j, offset;
char c;
int nInst, inst;
DDRC = 0xFF;
initialize();
PORTC = 0xFF;
r_count = 15;
offset = 0;
for (j = 0; j < 512; j++)
{
buffer[j] = 0;
}
MmcInit();
while (1)
{
// get the hello
gets_int(1);
// respond
putchar('!');
// get the # of instructions
gets_int(2);
nInst = ((int)r_buffer[0] << 8) | (r_buffer[1] << 0);
// respond
putchar('!');
j = 1;
buffer[0] = nInst >> 7;
buffer[1] = nInst << 1;
// ignore the first instruction
// it is 0x0000
gets_int(2);
// respond OK
putchar('+');
for (i = 1; i < nInst+1; i++)
{
// get an instruction
gets_int(2);
inst = ((int)r_buffer[0] << 8) | (r_buffer[1] << 0);
// put it in the buffer
buffer[(j << 1) + 0] = r_buffer[0];
buffer[(j << 1) + 1] = r_buffer[1];
// if we have received 256 instructions
// write them to flash
if (++j == 256)
{
// write the buffer to the memcard
mmc_write_sector(offset, buffer);
// zero out the buffer
for (j = 0; j < 512; j++)
{
buffer[j] = 0;
}
j = 0;
offset += 512;
}
// respond OK
putchar('+');
}
if (j != 0)
{
// Write any instructions that didnt fully fill at 512byte buffer
mmc_write_sector(offset, buffer);
// zero out the buffer
for (j = 0; j < 512; j++)
{
buffer[j] = 0;
}
}
// get terminating bytes
gets_int(2);
// Signal that you are ready for more
PORTC = 0b01010101;
}
}
void initialize(void)
{
//serial setup for debugging using printf, etc.
UCSRB = 0x18;
// we run at 19200 instead of the slow 9600
UBRRL = 51;
#asm("sei");
}
void gets_int(int count)
{
r_ready=0;
r_index=0;
UCSRB.7=1;
r_count = count;
while (r_index != r_count);
}
/*
Interrupts
*/
//UART character-ready ISR
interrupt [USART_RXC] void uart_rec(void)
{
r_char = UDR; //get a char
//build the input string
r_buffer[r_index++] = r_char;
if (r_index == r_count)
{
r_buffer[r_index] = 0x00;
r_ready = 1;
UCSRB.7 = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -