• 关于atmega32u4的CDC模拟串口程序


    编程环境使用的是AVR studio

    下载程序在这个链接里https://www.pjrc.com/teensy/usb_serial.html

    关键是电脑要安装驱动才能正常通信

     1 include <avr/io.h>
     2 #include <avr/pgmspace.h>
     3 #include <stdint.h>
     4 #include <util/delay.h>
     5 #include <stdio.h>
     6 #include <string.h>
     7 #include "usb_serial.h"
     8 
     9 
    10 #define LED_CONFIG    (DDRD |= (1<<6))
    11 #define LED_ON        (PORTD |= (1<<6))
    12 #define LED_OFF        (PORTD &= ~(1<<6))
    13 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
    14 
    15 void send_str(const char *s);
    16 
    17 int main(void)
    18 {
    19     CPU_PRESCALE(0);
    20     
    21     LED_CONFIG;//输出拉低
    22     LED_OFF;
    23 
    24     usb_init();
    25 
    26     while(1)
    27     {
    28         unsigned char buffer[3] = {0};
    29         
    30         int n = usb_serial_available();//获取缓存区中的字节数
    31     
    32         if(n>3 || (n<3 && n!=0))
    33         {
    34             send_str(PSTR(" please enter 3 words
    "));        
    35             n = 0;
    36             usb_serial_flush_input();
    37         }
    38         else
    39         {
    40             for(int i = 0;i<n;i++)
    41             {
    42                 int m = usb_serial_getchar();
    43                 if(m>=0)
    44                 {
    45                     buffer[i] = m;
    46                 }
    47             }
    48         }
    49         if (buffer[0]== 0xAA && buffer[2] == 0x55)
    50         {
    51             switch(buffer[1])
    52             {
    53                 case 0x31:
    54 
    55                 //    DDRD |= (1<<6);//输出拉高
    56                 //    LED_ON;
    57                     send_str(PSTR(" OPEN RHP-260+
    "));
    58                 break;
    59 
    60                 case 0x32:
    61 
    62                 //    DDRD |= (1<<6);//输出拉低
    63                 //    LED_OFF;
    64                     memset(buffer,0,3);
    65                     send_str(PSTR(" OPEN RHP-700+
    "));
    66                 break;
    67             
    68                 default:
    69                     usb_serial_putchar(buffer[1]);
    70                     send_str(PSTR(" wrong
    "));
    71                     memset(buffer,0,3);
    72                 break;
    73             }
    74         }
    75     }
    76 }
    77 
    78 // Send a string to the USB serial port.  The string must be in
    79 // flash memory, using PSTR
    80 //
    81 void send_str(const char *s)
    82 {
    83     char c;
    84     while (1) {
    85         c = pgm_read_byte(s++);
    86         if (!c) break;
    87         usb_serial_putchar(c);
    88     }
    89 }

    头文件usb_serial.h和usb_serial.c 是使用上面网页下载的,没有改动,只改了主函数,串口指令控制点灯

  • 相关阅读:
    tar命令解压jdk.tar.gz包 报错 gzip: stdin: not in gzip format
    CentOS6.5安装完没有网络的解决办法
    UML在需求分析阶段的应用
    UML
    UML在软件开发中各个阶段的作用和意义
    Maven那点事儿(Eclipse版)
    eclipse和myeclipse的下载地址
    div的作用
    c:if标签数据回显判断是否选中
    《Hadoop》对于高级编程Hadoop实现构建企业级安全解决方案
  • 原文地址:https://www.cnblogs.com/yaoshunyu/p/12921649.html
Copyright © 2020-2023  润新知