⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vbware.c

📁 上位机VB界面示波器 可用与自制串口通信 让信号的波形绘制与界面
💻 C
字号:
#include<mega16.h> 
#include<delay.h>
#define uchar unsigned char 
#define uint unsigned  int 
#define  URSEL    7 
#define  UCSZ0    1
#define  RXC  7  
#define  UDRE 5  
#define  counter_d 750
uchar data_s[750]; 
uchar falg=0; 
uint j=0;
void port_init(void)
{
 PORTA = 0xff;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}

//UART0 initialize
// desired baud rate: 19200
// actual: baud rate:19231 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
 UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x00;  
 UCSRC = (1<<URSEL) | 0x06; 
   UBRRL = 0x19;
 UBRRH = 0x00; //set baud rate hi
 UCSRB = 0x98; 
 SREG=0x80;
}  
void usart_putchar(uchar c)  //注意字字节形式 ,二进制数
{
//while(!UCSRA&(1<<UDRE)) ;    //等待寄存器中发送缓冲器空,使能寄存器中断   
UDR=c;                       //将数据放入缓冲器,发送
delay_us(360);               //延时时间10/19200即有大于520us发送一次                    
}

interrupt [13]void uart0_udre_isr(void)
{
 //character transferred to shift register so UDR is now empty 
  UDR=data_s[j];
  j++; 
  if(j>counter_d)
        { 
         UCSRB = 0x98;
         j=0;
         }
}  
                 

interrupt [12]void uart0_rx_isr(void)
{
  falg=UDR; 
}   
void ad_init(void)
{                                      

 ADMUX=0x07;  //选择7道ADC1//
 ADCSRA=0xe1; //自由模式,AD时钟2分频至少65us转换一次
}   
uint adc_convert(void)
{  
uint t;
uchar  ad_temp=0;
uchar temp1;
uint temp2;
temp1=ADCL; 
temp2=ADCH; 
temp2=temp2<<8;
t=temp2+temp1;
ad_temp=t/4;
return(ad_temp);
 }  
 void main(void)
 {   uint i;  
    port_init();
    uart0_init();
    ad_init();
    while(1)
    {
        for(i=0;i<counter_d;i++)
           { 
             
             data_s[i]=adc_convert(); 
             delay_us(70);    //AD时钟2分频至少65us转换一次
           }
    if (falg==111)
       { 
         if(j<counter_d)
         {
         UCSRB = 0xB8;
         }   //寄存器中发送缓冲器空,使能寄存器中断  
         while(j<counter_d)
         { 
           ;
          }
       } 
    }
    
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -