zoukankan      html  css  js  c++  java
  • 蓝牙2.0传数据给数码管

    蓝牙2.0是接收的无线信号,直接就发送到了单片机的串口,蓝牙2.0是自带驱动的,不需要写程序,但需要检测,用串口助手

    注意变量的替换,段码,位码,初始化缓存要用不同的英文来表示

    有缺陷的代码


    #include<STC15F2K60S2.H>
    #include"intrins.h"
    #define uint unsigned int
    #define uchar unsigned char
    #define led_output P0
    #define led_duan_and_wei P2 //共阳极数码管

    unsigned char add;

    unsigned char uart_index;

    uchar code tabe[ ]={
    0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90, //显示段选0-9
    0x88,0x83,0xc6,0xa1,0x86,0x8e, //显示段选A-F
    };
    uchar code tabe1[ ]={
    0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01
    }; // 显示位选第一位到第八位
    uchar tabe2[ ]={
    0xc0,0xc0,0xc0,0xc0,0xc0,0xa3,0xab,0xc0
    }; //初始化显示缓冲区

    void init_timer_T1_function(void)
    {
    SCON=0X50;
    TMOD=0X20;

    TH1=0xFD;//设置波特率9600
    TL1=0xFD;

    ES=1;
    TR1=1;
    }

    void main( )
    {

    init_timer_T1_function();

    EA=1;

    while(1)
    {
    led_output=tabe1[add];//选位选的位
    led_duan_and_wei = 0xdf;//打开位选控制Y6,送数据
    led_duan_and_wei = 0x1f;//关闭U8锁存器,锁存数据

    led_output=tabe2[add];//取余选择亮的段选数据
    led_duan_and_wei = 0xff;//打开段选控制Y7信号
    led_duan_and_wei = 0x1f; //清零,关闭U7锁存器

    add++;
    add&=0x07;

    }
    }

    void interrupt_ES_function() interrupt 4
    {


    if(RI)
    {
    RI=0;

    if(SBUF=='O')
    {
    uart_index=1;
    }
    else if(SBUF=='N'&&uart_index==1)
    {
    uart_index=2;
    }
    else if(uart_index==2)
    {
    uart_index=0;

    switch(SBUF)
    {

    case '1': break;
    case '2': break;
    case '3': break;
    case '4': break;
    case '5': break;
    case '6': break;
    case '7': break;
    case '8': break;
    case '9': break;
    case 'A': break;
    case 'B': break;
    case 'C': break;
    case 'D': break;
    case 'E': break;
    default:SBUF='0'; break;

    }

    tabe2[0]=tabe[SBUF%10];
    }

    }

    if(TI)
    {
    TI=0;
    }
    }

    纠正的代码

    #include<STC15F2K60S2.H>
    #include"intrins.h"
    #include"string.h"
    #define uint unsigned int
    #define uchar unsigned char //预处理
    #define led_output P0
    #define led_duan_and_wei P2 //共阳极数码管

    unsigned int uart_data1;
    unsigned char led_add;
    unsigned char uart_index;

    uchar code led_line[ ]={
    0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90, //显示段选0-9
    0x88,0x83,0xc6,0xa1,0x86,0x8e, //显示段选A-F
    };
    uchar code led_column[ ]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; // 显示位选第八位到第一位
    uchar led_cache[ ]={0xc0,0xa3,0xab,0xc0,0xc0,0xc0,0xc0,0xc0}; //初始化显示缓冲区

    void init_timer_T1_function(void)//初始化
    {
    EA=1; //开总中断
    ES=1; //开串口中断
    PCON=0x00;
    SCON=0X50;//串口工作方式1,并且启动串行接收
    TMOD=0X20;//定时器1工作在方式2

    TH1=0xFD;//设置波特率9600
    TL1=0xFD;
    TR1=1; //定时器1工作
    }

    void main( )
    {
    init_timer_T1_function();
    EA=1;

    while(1)
    {
    led_output=0;//消影
    led_duan_and_wei=0xff;//
    led_duan_and_wei=0x1f;

    led_output=led_column[led_add];//选位选的位
    led_duan_and_wei=0xdf;//打开位选控制Y6,送数据
    led_duan_and_wei=0x1f;//关闭U8锁存器,锁存数据

    led_output=led_cache[led_add];//取余选择亮的段选数据
    led_duan_and_wei=0xff;//打开段选控制Y7信号
    led_duan_and_wei=0x1f; //清零,关闭U7锁存器

    led_add++;
    led_add&=0x07;
    }
    }

    void interrupt_ES_function() interrupt 4 //中断4:串口收发数据
    {
    EA=0;
    if(RI)
    {
    RI=0;
    uart_data1=SBUF;

    if(uart_data1=='O')
    {
    uart_index=1;
    }
    else if(uart_data1=='N'&&uart_index==1)
    {
    uart_index=2;
    }
    else if(uart_index==2)
    {
    uart_index=0;

    switch(uart_data1)
    {
    case '1': break;
    case '2': break;
    case '3': break;
    case '4': break;
    case '5': break;
    case '6': break;
    case '7': break;
    case '8': break;
    case '9': break;
    case 'A': break;
    case 'B': break;
    case 'C': break;
    case 'D': break;
    case 'E': break;
    default:uart_data1='F'; break;
    }
    if(uart_data1>='A')//ASCII转为十进制
    {
    uart_data1=uart_data1-55;
    }
    else if(uart_data1>='0')
    {
    uart_data1=uart_data1-48;//0
    }
    led_cache[0]=led_line[uart_data1%16];//16进制
    }
    }
    if(TI)
    {
    TI=0;
    }
    EA=1;
    }

  • 相关阅读:
    Angular2学习笔记四(之Http通信)
    立即掌握SSM框架的要诀
    How to delete the eclipse plugin.
    eclipse(Version: Mars.2 Release (4.5.2)) groovy plugin install process.
    初学ssm框架的信息
    Maven安装教程
    基于Java Mail 进行发送(带附件和压缩附件)的邮件
    基于POI和DOM4将Excel(2007)文档写进Xml文件
    datatables 行创建回调
    datatables表格中 嵌套表格问题
  • 原文地址:https://www.cnblogs.com/296389183yy/p/8800693.html
Copyright © 2011-2022 走看看