📄 notrepad01.c
字号:
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <inttypes.h>
//#include <stdlib.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'};
//*********** a/d Function *******************//
#define ExtVRF 1 // 1 external VoltageReferance
#define IntVRF 0 // 0 internal VoltageReferance
//*********** 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);
/***********************************************************/
SIGNAL(SIG_ADC) {
unsigned char AD_LSB,AD_MSB;
AD_LSB = ADCL;
AD_MSB = ADCH;
analog_result=((0x03 & AD_MSB)<<8)|(AD_LSB & 0xFF);
analog_busy=0;
}
extern int ADC_read(unsigned char channel,unsigned char iref)
{
analog_busy=1;
/* enable analog to digital conversion in single run mode
* without noise canceler function. See datasheet of atmega8 page 195
* We set ADPS2=1 and ADPS0,ADPS1=0 to have a clock division factor of 32.
* This is needed to stay in the recommended range of 50-200kHz
* ADEN: Analog Digital Converter Enable
* ADIE: ADC Interrupt Enable
* ADIF: ADC Interrupt Flag
* ADCSR: ADC Control and Status Register
* ADPS2..ADPS0: ADC Prescaler Select Bits
* REFS: Reference Selection Bits (page 203)
*/
// int-ref with external capacitor at AREF pin:
// 2.56V int ref=REFS1=1,REFS0=1
// write only the lower 3 bit for channel selection
if (iref){ // 2.56V ref
ADMUX = (1<<REFS1) | (1<<REFS0) | (channel & 0x07);
}else{ // 5V ref
ADMUX = (1<<REFS0) | (channel & 0x07);
}
ADCSRA = (1<<ADEN) | (1<<ADIE) | (1<<ADIF) | (1<<ADPS2);
/* start conversion */
ADCSRA |= (1<<ADSC);
while(analog_busy); /* wait for result */
return(analog_result);
}
//*********** End a/d **************************//
int main (void)
{
unsigned int values;
unsigned char c,indexBuffer,compleate=0;
unsigned char portBuffer0=0,portBuffer1=0;
float resaults;
//Timer0_Init();
//milseconds = 0;
DDRB = 0b00001111;
PORTB = 0b00001111;
DDRD = 0b11111000;
PORTD = 0b11111000;
timerFlag = 0;
sei();
// USART_Init(USART_BAUD_SELECT(USART_BAUD_RATE,F_CPU));
USART_Init(USART_BAUD_SELECT);
usart_puts(" test UART ATMEGA8 \n\r");
/*
while(1)
{
usart_puts(" test UART ATMEGA8 \n\r");
delay_ms(500);
}
*/
while(1)
{
PORTB = 0b00000000;
delay_ms(150);
PORTB = 0b00000001;
delay_ms(150);
PORTB = 0b00000010;
delay_ms(150);
PORTB = 0b00000011;
delay_ms(150);
PORTB = 0b00000100;
delay_ms(150);
PORTB = 0b00000101;
delay_ms(150);
PORTB = 0b00000110;
delay_ms(150);
PORTB = 0b00000111;
delay_ms(150);
PORTB = 0b00001000;
delay_ms(150);
PORTB = 0b00001001;
delay_ms(150);
PORTB = 0b00001010;
delay_ms(150);
PORTB = 0b00001011;
delay_ms(150);
PORTB = 0b00001100;
delay_ms(150);
PORTB = 0b00001101;
delay_ms(150);
PORTB = 0b00001110;
delay_ms(150);
PORTB = 0b00001111;
delay_ms(150);
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 ## Read A/D ch-1 ## \n\r");
do{
if(milseconds==500)
{
milseconds=0;
values = ADC_read(1,ExtVRF);
resaults = (float) (values *5) / 1023 ;
//usart_SendFloat(resaults,1,3);
//usart_puts("\n");
}
}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 --> [ A/D 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 */
unsigned char StringToHex(unsigned char text)
{
unsigned char ca=0;
switch(text)
{
case '0' : ca = 0; break;
case '1' : ca = 1; break;
case '2' : ca = 2; break;
case '3' : ca = 3; break;
case '4' : ca = 4; break;
case '5' : ca = 5; break;
case '6' : ca = 6; break;
case '7' : ca = 7; break;
case '8' : ca = 8; break;
case '9' : ca = 9; break;
case 'A' : ca = 10; break;
case 'B' : ca = 11; break;
case 'C' : ca = 12; break;
case 'D' : ca = 13; break;
case 'E' : ca = 14; break;
case 'F' : ca = 15; break;
}
return ca;
}
void PortControl(unsigned char MSBbyte,unsigned char LSBbyte)
{
bit_write(PORTB,0,(chkbit(MSBbyte,3)));
bit_write(PORTB,1,(chkbit(MSBbyte,2)));
bit_write(PORTB,2,(chkbit(MSBbyte,1)));
bit_write(PORTD,3,(chkbit(MSBbyte,0)));
bit_write(PORTD,4,(chkbit(LSBbyte,3)));
bit_write(PORTD,5,(chkbit(LSBbyte,2)));
bit_write(PORTD,6,(chkbit(LSBbyte,1)));
bit_write(PORTD,7,(chkbit(LSBbyte,0)));
}
//******************************** END *******************************************//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -