|
由于时间不多,好不容易写好了这部分,就是ADC(仅温度),把采到的数据通过RS232发给PC,上位采集我想用DELPHI写,我第一次写串口的程序,有没有哪位用DELPHI的可以给点好的建议?
下面给出单片机的程序,原著donkey,修改xxzhang,:)
//ICC-AVR application builder : 2005-1-7 1:32:58
// Target : M16
// Crystal: 8.0000Mhz
#include <iom16v.h>
#include <macros.h>
#include <stdio.h>
void port_init(void);
void uart0_init(void);
void adc_init(void);
void init_devices(void);
void Temperature(int tp);
void delay_1ms(void); //函数声明,本实验中会用到这两个函数
void delay_nms(unsigned int n);
main()
{
short int c;
init_devices();
DDRA=0B11000000; //定义A口的PA6、PA7为输出口
PORTA=0B10000000; //PA7口输出高电平,PA6口输出低电平
delay_nms(5000); //延时5秒
PORTA=0B01000000; //PB6口输出高电平,PB7口输出低电平
delay_nms(5000); //延时5秒
//uart0_init();
PORTA=0B10000000;
c=0xa8;
putchar(c);
PORTA=0B01000000; //PA6口输出高电平,PA7口输出低电平
while(1)
{
//c=getchar();
//putchar(c);
Temperature(1);
Temperature(2);
delay_nms(100);
PORTA^=0B11000000;
// USART_Transmit(0xff);
// delay_nms(50);
}
}
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//UART0 initialize
// desired baud rate: 38.4k
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
// UBRRL = 0x33; //set baud rate lo
UBRRL = 12;
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x18;
}
//ADC initialize
// Conversion time: 104uS
void adc_init(void)
{
ADCSR = 0x00; //disable adc
ADMUX = 0x20; //select adc input 0
ACSR = 0x80;
ADCSR = 0x86;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
uart0_init();
adc_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void Temperature(int tp)
{
unsigned char tl, th;
while(ADCSR&(1<<ADSC));
if(tp==1) ADMUX=0x25;
else ADMUX=0x24;
ADCSR|=0x40;
while(ADCSR&(1<<ADIF));
tl=ADCL;
th=ADCH;
if(tp==1)
{
//puts("Tempurature1 :");
putchar(th);
putchar(tl);
putchar(0xff);
}
else
{
//puts("Tempurature2 :");
putchar(th);
putchar(tl);
putchar(0xff);
};
}
void delay_1ms(void)//1ms延时函数
{
unsigned int i;
for (i=0;i<150;i++)
{
}
}
void delay_nms(unsigned int n)//延时n毫秒
{
unsigned int i;
for (i=0;i<n;i++)//执行n次1毫秒延时
delay_1ms();
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|