//ICC-AVR application builder : 2006-3-17 15:44:36
// Target : M48
// Crystal: 8.0000Mhz
///测试台
#include <iom48v.h>
#include <macros.h>
#include <stdio.h>
#define CHUNNEL 9
unsigned int Adc_Result[CHUNNEL]={0};
unsigned char whole_flag=0;
void int_printf(int);
void char_printf(char);
#define TRUE 1
#define FALSE 0
/////////////串口通信常数定义
#define CONNECT 10
#define SUCCEED 20
#define DISABLE 30
#define UPLOAD 40
#define ADC_START 0x40
//数字滤波次数选择
#define N 16
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x04;
DDRD = 0xF0;
}
//TIMER1 initialize - prescale:Stop
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Hz
// actual value: Out of range
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xe8;//f0; //setup///1S定时
TCNT1L = 0xe0;
OCR1AH = 0x0F;
OCR1AL = 0x42;
OCR1BH = 0x0F;
OCR1BL = 0x42;
ICR1H = 0x0F;
ICR1L = 0x42;
TCCR1A = 0x00;
TCCR1B = 0x00; //start Timer
}
//UART0 initialize
// desired baud rate: 19200
// actual: baud rate:19231 (0.2%)
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x0c; //set baud rate lo//19.2K
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x98;
}
//ADC initialize
// Conversion time: 6uS
void adc_init(void)
{
ADCSRA = 0x00; //disable adc
ADMUX = 0xc0; //select adc input 0(c是1.1V,0是AREF)
ACSR = 0x80;
ADCSRB = 0x00;
DIDR0 = 0X3F;
ADCSRA = 0x84; ///|0X40启动一次ADC转换
}
//call this routine to initialize all peripherals
void init_devices(void)
{
CLKPR=0x80;
CLKPR=0x01;
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
uart0_init();
adc_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK0 = 0x00; //timer 0 interrupt sources
TIMSK1 = 0x01; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void delay()
{
char n=255;
while(--n);
}
void delay_long(int x)
{
while(--x) delay();
}
///数字滤波//////////////////////////////////////////////
int filter()
{
unsigned int count,i,j,temp;
unsigned int value_buf[N];
unsigned int sum=0;
for (count=0;count<N;count++)
{
while(ADCSRA&ADC_START);
value_buf[count] = ADC;
ADCSRA|=ADC_START;
}
for (j=0;j<N-1;j++)
{
for (i=0;i<N-j;i++)
{
if ( value_buf[i]>value_buf[i+1] )
{
temp = value_buf[i];
value_buf[i] = value_buf[i+1];
value_buf[i+1] = temp;
}
}
}
for(count=1;count<N-1;count++)
sum += value_buf[count];
return (int)((sum<<2)/(N-2));//超采2位
}
////////////////ADC通道选择/////////////////////////////
void ADC_Input_Select()
{
unsigned char select=0;
unsigned int temp;
for(select=0;select<(CHUNNEL-1);select++)
{
ADMUX&=0xf0;
ADMUX|=(select&0x0f);
ADCSRA|=ADC_START;
//temp=ADC;
while(ADCSRA&ADC_START);
ADCSRA|=ADC_START;
while(ADCSRA&ADC_START);
ADCSRA|=ADC_START;
while(ADCSRA&ADC_START);
ADCSRA|=ADC_START;
while(ADCSRA&ADC_START);
ADCSRA|=ADC_START;
temp=filter();
Adc_Result[select]=temp;
if(select==5)/////////////更换通道
{
ADMUX&=0xf0;
ADMUX|=3;
ADCSRA|=ADC_START;
while(ADCSRA&ADC_START);
ADCSRA|=ADC_START;
while(ADCSRA&ADC_START);
ADCSRA|=ADC_START;
Adc_Result[5]=filter();
}/////更换通道
if(select==3)
{
ADMUX&=0xf0;
ADMUX=2;
PORTD|=0X80;/////接负载
delay_long(12000);
ADCSRA|=ADC_START;
delay();delay();delay();delay();
Adc_Result[CHUNNEL-1]=filter();///接入负载后测它的400V电压
}//if
}//for
}
/////////////////////////1S定时器////////////////////////
#pragma interrupt_handler timer1_ovf_isr:14
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0xe8;//f0; //reload counter high value////1S定时
TCNT1L = 0xe0; //reload counter low value
if(whole_flag==0){PORTD|=0x40;whole_flag++;return;}
if(whole_flag==1)
{
TCCR1B = 0x00;
ADC_Input_Select();
whole_flag++;
}//if
}
////////////////////串口通信////////////////////////////////
void char_printf(char chdata)
{
while(!(UCSR0A&(1<<UDRE0)));
UDR0=chdata;
}
void int_printf(int intdata)///先高位后低位传送
{
unsigned char temp;
temp=(unsigned char)(intdata>>8);
char_printf(temp);
temp=(unsigned char)intdata;
char_printf(temp);
}
//////////上传测试数据
void up_load()
{
unsigned char x=0;
for(;x<CHUNNEL;x++)
{
int_printf(Adc_Result[x]);
}
}
//////接收中断服务程序////////////////////
#pragma interrupt_handler uart0_rx_isr:19
void uart0_rx_isr(void)
{
//uart has received a character in UDR
unsigned char chtemp;
chtemp=UDR0;
switch(chtemp)
{
case CONNECT:
{
char_printf(SUCCEED);///成功
break;
}
case SUCCEED:
{
up_load();
char_printf(TRUE);
break;
}
case DISABLE:
{
char_printf(UPLOAD);
break;
}
case UPLOAD:
{
break;
}
default:{return;}
}//switch
}
/*
#pragma interrupt_handler uart0_tx_isr:21
void uart0_tx_isr(void)
{
//printf("%u /n,xx");
UDR0=0X09;
//character has been transmitted
}*/
void main()
{
//unsigned int xx=2109;
init_devices();
////////////////////////////
//while(1)int_printf(xx);
while(1)
{
if(!(PIND&0x04))///Pcb板触发信号
{
delay();
if((PIND&0x04)==0)///Pcb板触发信号
{
PORTD|=0X20;///装PCB
TCCR1B = 0x05; //start Timer=0x05;256
while(!(whole_flag==2));
whole_flag=0;
PORTD&=0x7f;///断负载
PORTD&=0Xbf;///断电源
delay_long(650);
PORTD|=0X10;///电容放电///////////////////////////////////
PORTD&=0Xdf;///卸PCB
///////////////
//上传数据
char_printf(UPLOAD);//
////////
w1:
while(!(PIND&0x04));
delay_long(1000);
delay();delay();delay();delay();delay();
delay();delay();delay();delay();delay();
if(!(PIND&0x04)) goto w1;
PORTD&=0Xef;///电容放电结束
}
}//if
}//while
while(1);
}
HID_test.rar_atmega48_atmega48 程序_hid_hid ballast
版权申诉
48 浏览量
2022-09-22
20:35:25
上传
评论
收藏 69KB RAR 举报
朱moyimi
- 粉丝: 64
- 资源: 1万+
最新资源
- JAVA实现Modbus RTU或Modbus TCPIP案例.zip
- 基于YOLOv8的FPS TPS AI自动锁定源码+使用步骤说明.zip
- JAVA实现Modbus RTU或Modbus TCPIP案例.zip
- 基于yolov8+streamlit的火灾检测部署源码+模型.zip
- 测试aaaaaaabbbbb
- VID20240521070643.mp4
- Android系统原理与开发学习要点详解-培训课件.zip
- 部署yolov8的tensorrt模型支持检测分割姿态估计的C++源码+部署步骤.zip
- 以简单、易用、高性能为目标、开源的时序数据库,支持Linux及Windows, Time Series Database.zip
- python-leetcode面试题解之第198题打家劫舍-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈