//*********ATmega128,8channel ADC sampling + LED display + USART*******
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define FOSC 1843200
#define baud 9600
#define MyUBBR (uint)((ulong)FOSC/(16*(ulong)baud)-1)
const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar Data[4]={0};
uchar adcon0_table[]={0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47};
uchar num1,num2;
uint data_buf[10]={0};
void delay_ms(uint x);
void PORT_init();
void USART_Init(uint ubbr);
void USART_Transmit(uchar data);
uchar USART_Receive(void);
void NumericalLED_display(void);
uint data_average(uint buffer[30]);
void get_AD(void);
void main()
{
uchar i;
PORT_init();
USART_Init(MyUBBR);
while(1)
{
NumericalLED_display();
get_AD();
if(USART_Receive()==0x01)
{
num1=PORTD;
num2=PORTB&0X03;
if((num1==0xff)&&(num2==0x03))
{
USART_Transmit(num1);
USART_Transmit(num2);
for(i=0;i<8;i++)
{
num1=data_buf[i]/255;
num2=data_buf[i]%255;
USART_Transmit(num1);
USART_Transmit(num2);
delay_ms(1000);
}
}
}
}
}
void delay_ms(uint x)
{
uint y,z;
for(y=x;y>0;y--)
for(z=110;z>0;z--);
}
void PORT_init()
{
DDRA=0x00;
DDRB=0xFF;
PORTB=0xFF;
DDRC=0xFF;
PORTC=0xFF;
PORTD=0xFF;
DDRD=0xFF;
}
void USART_Init(uint ubbr)
{
UBRR1H= (uchar)(ubbr>>8);
UBRR1L= (uchar)ubbr;
UCSR1A= 0x00;
UCSR1B|=(1<
}
void USART_Transmit(uchar data)
{
while(!(UCSR1A&(UDRE1)));
UDR1=data;
}
uchar USART_Receive(void)
{
while(!(UCSR1A&(1<
}
void NumericalLED_display(void)
{
static uchar i,temp=0x01;//temp=0x7f;
for (i=0; i<4; i++)
{
PORTC=table[Data[i]];
temp=1< PORTB&=~temp;
delay_ms(10);
//PORTC=table[Data[i]];
//PORTB=temp;
//temp=(temp>>1)|0x80;
//__delay_cycles(100);
}
}
uint data_average(uint buffer[30])
{
uchar i,j;
uint temp;
float temp1;
for(i=1; i<30; i++)
for(j=29; j>=i; --j)
{
if(buffer[j-1] > buffer[j])
{
temp = buffer[j-1];
buffer[j-1] = buffer[j];
buffer[j] = temp;
}
}
temp1 = 0;
for(i=5; i<25; i++)
{
temp1 += buffer[i];
}
temp = (uint)(((float)temp1) / 20 + 0.5);
return(temp);
}
void get_AD(void)
{
uchar i,j;
uint adcbuf[30]={0};
static uchar ADC_channel=0;
//ADMUX=0x40;
ADCSRA=0x83;
SFIOR=0x00;
delay_ms(1);
for (i=0; i<8; i++)
{
ADMUX=adcon0_table[ADC_channel];
for(j=0; j<30; j++)
{
asm("NOP");
asm("NOP");
ADCSRA|=BIT(6);
while(((1<
adcbuf[j]=(ADCH<<8)|ADCL;
}
data_buf[i]=data_average(adcbuf);
Data[3]=data_buf[i]/1000;
Data[2]=data_buf[i]%1000/100;
Data[1]=data_buf[i]%100/10;
Data[0]=data_buf[i]%10;
if (++ADC_channel>8)ADC_channel=0;
delay_ms(1);
}
}
//PIC Multi_CD channel source code,
//******Multi_ADChannel converter**********//
#include
__PROG_CONFIG(1,0xc200);
__PROG_CONFIG(2,0x0a16);
__PROG_CONFIG(3,0x8100);
__PROG_CONFIG(4,0x0081);
__PROG_CONFIG(5,0xc00f);
__PROG_CONFIG(6,0xe00f);
__PROG_CONFIG(7,0x400f);
#define uchar unsigned char
#define uint unsigned int
uchar temp;
void multi_convertor(void);
void port_check(void);
uint ADbuf[10];
void delay_ms(uint t)
{
uint m,n;
for (m=0;m
}
void GPIO_Init(void)
{
TRISA=0xff;
TRISB=0xff;
TRISC=0xff;
TRISD=0xff;
TRISE=0xff;
PSPMODE=0;
PSPIE=0;
PSPIP=0;
TRISBbits.RB2 =1; //set direction of RB2 to be An input;
TRISBbits.RB3 =1; //set direction of RB3 to be An input;
}
void ADC_Init(void)
{
ADCON1 = 0x05; //seting AN0~AN9 to be analog input;
ADCON2 = 0xBE; //20TAD,FOSC/64 and right justified;
}
void timer0_init(void)
{
INTCON =0xE0;//GIE,PEIE,TMR0IE to set;
T0CON = 0x1F;// timer0 to be 16-bit;
TMR0H = (65536-1000)/256;
TMR0L = (65536-1000)%256;
T0CON |=0x80;
TMR0ON=1;
}
void EUART_init(void)
{
TRISC7=1;
TRISC6=1;
SPBRG=0x40;
TXSTA=0x24;
RCSTA=0x90;
RCIE=0;
TXIE=0;
PEIE=1;
GIE=1;
}
void transmit_udata(uchar udata)
{
TXREG=udata;
while(!TRMT);
}
uchar receive_udata(void)
{
uchar temp;
if (RCIF==1)
{
temp=RCREG;
}
return temp;
}
uint data_average(uint buffer[30])
{
uchar i,j;
uint temp;
float temp1;
for(i=1; i<30; i++)
for(j=29; j>=i; --j)
{
if(buffer[j-1] > buffer[j])
{
temp = buffer[j-1];
buffer[j-1] = buffer[j];
buffer[j] = temp;
}
}
temp1 = 0;
for(i=5; i<25; i++)
{
temp1 += buffer[i];
}
temp = (uint)(((float)temp1) / 20 + 0.5);
return(temp);
}
void main(void)
{
EUART_init();
GPIO_Init();
ADC_Init();
timer0_init();
while(1)
{
temp=receive_udata();
if (temp==0x01)
{
temp=0;
port_check();
multi_convertor();
}
}
}
void port_check(void)
{
uchar num1, num2;
num1=PORTD;
num2=PORTB&0X03;
transmit_udata(num2);
transmit_udata(num1);
}
void multi_convertor(void)
{
uchar data1,data2,i;
static uchar ADC_channel=0;
uint adc[30]={0};
// ADC_channel++;
for (ADC_channel=0; ADC_channel<10; ADC_channel++)
{
switch(ADC_channel)
{
case 0: ADCON0=0x01;break;//AN0 channel;
case 1: ADCON0=0x05;break;//AN1 channel;
case 2: ADCON0=0x09;break;//AN2 channel;
case 3: ADCON0=0x0D;break;//AN3 channel;
case 4: ADCON0=0x11;break;//AN4 channel;
case 5: ADCON0=0x15;break;//AN5 channel;
case 6: ADCON0=0x19;break;//AN6 channel;
case 7: ADCON0=0x1D;break;//AN7 channel;
case 8: ADCON0=0x21;break;//AN8 channel;
case 9: ADCON0=0x25;break;//AN9 channel;
default:break;
}
for(i=0;i<30;i++)
{
ADCON0 |=0x01;
asm("NOP");
asm("NOP");
GODONE=1;
while(GODONE);
adc[i]=(ADRESH*256)+ADRESL;
}
ADbuf[ADC_channel]=data_average(adc);
data1=ADbuf[ADC_channel]/255;
data2=ADbuf[ADC_channel]%255;
transmit_udata(data1);
transmit_udata(data2);
}
}
上一篇:DHT11温湿度传感器的AVR单片机例程
下一篇:avr单片机双通信C程序
设计资源 培训 开发板 精华推荐
- 使用 ROHM Semiconductor 的 BD46321 的参考设计
- 用于喷墨打印机的 312W、24V 交流转直流单输出电源
- 快速轻松地创建USB Type-C连接器参考设计
- 的卢飞快,沁恒RISC-V智能手环
- LM2931AD50R 具有抑制功能的超低压降稳压器的典型应用电路
- 使用 LTC3612IUDC 通用降压稳压器的典型应用,该稳压器采用陶瓷电容器,2.25MHz
- LTC1261LIMS8-4.5 1mV 纹波、5V 输入、-4V 输出 GaAs FET 偏置发生器的典型应用电路
- AD8531AKSZ-REEL7 单电源立体声耳机驱动器的典型应用
- 使用 Cypress Semiconductor 的 CY7C9689 的参考设计
- LTC3822,大电流应用在 15A 时提供 1.8V
- 睿瀚医疗万斌:“脑机接口+AI+机器人”是康复赛道的未来
- 希润医疗孟铭强:手功能软体机器人,让脑卒中患者重获新生
- 柔灵科技陈涵:将小型、柔性的脑机接口睡眠设备,做到千家万户
- 微灵医疗李骁健:脑机接口技术正在开启意识与AI融合的新纪元
- USB Type-C® 和 USB Power Delivery:专为扩展功率范围和电池供电型系统而设计
- 景昱医疗耿东:脑机接口DBS治疗技术已实现国产替代
- 首都医科大学王长明:针对癫痫的数字疗法已进入使用阶段
- 非常见问题解答第223期:如何在没有软启动方程的情况下测量和确定软启动时序?
- 兆易创新GD25/55全系列车规级SPI NOR Flash荣获ISO 26262 ASIL D功能安全认证证书
- 新型IsoVu™ 隔离电流探头:为电流测量带来全新维度