📄 save_main.c
字号:
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include "delay.h"
#include "timer0.h"
#include "myDef.h"
#include "uart.h"
const prog_char hex[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
//*********** SPI Function *******************//
#define DD_ENB DDB0 // ENB (EN) pin(PB0)
#define PORT_EN PORTB // SPI PORT EN
#define PIN_EN PINB0 // SPI PIN EN
#define DDR_SPI DDRB // SPI PORT PIN
#define DD_SS DDB4 // SS (SS) pin(PB4) // Not used for spi-16bit
#define DD_MOSI DDB5 // MOSI(DI) pin(PB5)
#define DD_SCK DDB7 // SCK (CLK) pin(PB7)
//*********** a/d Prototype ******************//
extern int ADC_read(unsigned char channel,unsigned char iref);
volatile static unsigned char analog_busy;
volatile static int analog_result;
volatile static unsigned char timerFlag=0;
unsigned char StringToHex(unsigned char text);
/**************** General Prototype ************************/
void PortControl(unsigned char MSBbyte,unsigned char LSBbyte);
void SPI_MasterTransmit(char cData);
void SPI_MasterInit(void);
int main (void)
{
unsigned char c,indexBuffer,compleate=0;
unsigned char portBuffer0=0,portBuffer1=0;
Timer0_Init();
milseconds = 0;
DDRB = 0b00001111;
PORTB = 0b00001111;
DDRD = 0b11111000;
PORTD = 0b11111000;
timerFlag = 0;
sei();
usart_init(USART_BAUD_SELECT);
usart_puts(" test UART ATMEGA8 \n\r");
while(1)
{
PORTB = 0b00000000;
delay_ms(500);
PORTB = 0b00000001;
delay_ms(500);
usart_puts(" test UART ATMEGA8 \n\r");
}
while(1)
{
while(DataInReceiveBuffer()) /* True if "Not" empty */
{
c = usart_getc();
if(c =='A') // Recieve "A" loop for read A?D ch-0
{
milseconds=0;
usart_puts("\n\r ## Recieve "A" ## \n\r");
do{
if(milseconds==500)
{
milseconds=0;
usart_puts("\n\r -- "A" ## \n\r");
}
}while(!DataInReceiveBuffer());
}// end Recieve "A"
else if(c =='P') // Recieve "P" + (MSB-byte) + (LSB Byte)
{
milseconds=0;
usart_puts("## Control Port ## --> ");
while(! (DataInReceiveBuffer()||(milseconds>=999)))
;
indexBuffer = 0;
compleate=0;
portBuffer0 = usart_getc();
milseconds=0;
indexBuffer=1;
while(!(DataInReceiveBuffer()||(milseconds>=999)))
;
if(DataInReceiveBuffer() && (milseconds < 999) )
{
portBuffer1 = usart_getc();
milseconds=0;
compleate=1;
}
if(compleate)
{
usart_putc(portBuffer0);
usart_putc(portBuffer1);
portBuffer0 = StringToHex(portBuffer0);
portBuffer1 = StringToHex(portBuffer1);
PortControl(portBuffer0,portBuffer1);
milseconds=0;
}else{
usart_puts("\n\r ## ERROR --> [ PortControl Communication-TimeOut] ");
milseconds=0;
}
}// end Recieve "P"
else if(c =='#') // Recieve "#" + chanel
{
milseconds=0;
usart_puts("\n\r ## Read A/D ch-");
while(! (DataInReceiveBuffer()||(milseconds>=999)))
;
if(DataInReceiveBuffer() && (milseconds < 999) )
{
c = 0x0F & usart_getc();
usart_putc(c+0x30);
usart_puts(" ## ---> values = ");
//values = ADC_read((c-0x30),ExtVRF);
//resaults = (float) (values *5) / 1023 ;
//usart_SendFloat(resaults,1,3);
//usart_16bitHex(values);
usart_puts(" Volts\n");
milseconds=0;
}else{
usart_puts("\n\r ## ERROR --> [Communication-TimeOut] ");
milseconds=0;
}
}// end Recieve "#"
else if(c =='B') // Recieve "B"
{
milseconds=0;
do{
if((milseconds==499) && (timerFlag==0))
{
PortControl(0x0A,0x0A);
timerFlag = 1;
}
if((milseconds==999) && (timerFlag==1))
{
PortControl(0x05,0x05);
timerFlag = 0;
milseconds=0;
}
}while(!DataInReceiveBuffer());
}// end Recieve "B"
else{
usart_putc(c);
}
}// end DataInReceiveBuffer()
}// end while(1)
return 0;
}/* Eend of main() loop */
//************************** Prototype *****************************//
void SPI_MasterInit(void)
{
// Set MOSI, SCK and ENB output, all other input
DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_ENB)|(1<<DD_SS);
// Enable SPI, Master, set clock rate fck/64
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0);
}
void SPI_MasterTransmit(char cData)
{
// Start transmission
SPDR = cData;
// Wait for transmission complete
while (!(SPSR & (1<<SPIF)))
;
}
//******************************** END *******************************************//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -