找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1117|回复: 4
打印 上一主题 下一主题
收起左侧

请教一下论坛里的大神i能不能帮我看看我代码错在哪儿

[复制链接]
跳转到指定楼层
楼主
#include "stc15f2k60s2.h"
#include "lcd.h"
#include <stdio.h>
#include <string.h>
#include "intrins.h"
#define uint unsigned int
#define uchar unsigned char
int i;

uint code zhiling[ ]={0xDD,0xA5,0x03,0x00,0xFF,0xFD,0x77};

char xdata char_U[50];                        //总电压
char xdata char_I[50];                        //总电流
char xdata char_EXT[50];                //电池温度
void display();                                        //屏幕显示
sbit newTXD = P4^1;             //发送端设为P4.1
sbit newRXD = P4^2;             //接收端设为p4.2

void Delayxms(unsigned char i)
{
while(--i)
{
_nop_();
}
}



void UartInit()                 //串口初始化
{TMOD= 0x02;           //定时器0方式2
        TH0 = 0xFD;           //9600波特率
        TL0 = TH0;
        TR0 = 0;                //在发送或接受才开始使用
        TF0 = 0;                                                       
        ET0 = 1;                //允许允许定时器0中断
        EA=1;                        //中断允许
}

void WByte(uint input)
{
                         //发送启始位
     uchar j=8;
     newTXD=(bit)0;               //发送8位数据位
     while(j--)
     {
         newTXD=(bit)(input&0x01);      //先传低位
         input=input>>1;
     }

                        //发送校验位(无)

                        //发送结束位
    newTXD=(bit)1;
}   

void Sendata()
{
     for(i=0;i<sizeof(zhiling);i++)//外层循环,遍历数组
    {
          WByte(zhiling[i]);
     }
}

void display()
{
display_graphic_16x16(1,1,0,zong16);
display_graphic_16x16(1,17,0,dian16);
display_graphic_16x16(1,33,0,ya16);  
display_string_8x16(1,49,0,":");   //总电压
display_graphic_16x16(3,1,0,zong16);
display_graphic_16x16(3,17,0,dian16);
display_graphic_16x16(3,33,0,liu16);  
display_string_8x16(3,49,0,":");    //总电流
display_graphic_16x16(5,1,0,sheng16);
display_graphic_16x16(5,17,0,yu16);
display_graphic_16x16(5,33,0,dian16);
display_graphic_16x16(5,49,0,liang16);   
display_string_8x16(5,65,0,":");     //剩余电量
display_graphic_16x16(7,1,0,dian16);
display_graphic_16x16(7,17,0,chi16);
display_graphic_16x16(7,33,0,wen16);
display_graphic_16x16(7,49,0,du16);
display_string_8x16(7,65,0,":");     //电池温度
}

void main()
{
     UartInit();
     while(1)
     {
          Sendata();
     }
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:379392 发表于 2018-8-22 11:28 | 只看该作者
第8行 int i ; 应该是uint i;另外i是不是重复定义了  程序不全 display()子程序没用到 而且主程序前面的子程序不需要声明
回复

使用道具 举报

板凳
ID:381738 发表于 2018-8-22 13:47 | 只看该作者
santo 发表于 2018-8-22 11:28
第8行 int i ; 应该是uint i;另外i是不是重复定义了  程序不全 display()子程序没用到 而且主程序前面 ...

问题是我设置的是波特率9600,按理说应该是104微秒一位,实际情况并不是,示波器上波形很密集,还有就是按照通讯协议发送的指令,然而没有得到回复,是两个单片机之间的通讯,用的io模拟串口,这个是通讯盒上的程序
回复

使用道具 举报

地板
ID:155507 发表于 2018-8-22 15:45 | 只看该作者
给你改了一下试试。

  1. #include "stc15f2k60s2.h"
  2. #include "lcd.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "intrins.h"
  6. #define uint unsigned int
  7. #define uchar unsigned char
  8. int i;

  9. uint code zhiling[ ]={0xDD,0xA5,0x03,0x00,0xFF,0xFD,0x77};

  10. char xdata char_U[50];                        //总电压
  11. char xdata char_I[50];                        //总电流
  12. char xdata char_EXT[50];                //电池温度
  13. void display();                                        //屏幕显示
  14. sbit newTXD = P4^1;             //发送端设为P4.1
  15. sbit newRXD = P4^2;             //接收端设为p4.2

  16. void Delayxms(unsigned char i)
  17. {
  18. while(--i)
  19. {
  20. _nop_();
  21. }
  22. }



  23. void UartInit()                 //串口初始化
  24. {TMOD= 0x02;           //定时器0方式2
  25.         TH0 = 0xFD;           //9600波特率
  26.         TL0 = TH0;
  27.         TR0 = 0;                //在发送或接受才开始使用
  28.         TF0 = 0;                                                        
  29.         ET0 = 1;                //允许允许定时器0中断
  30.         EA=1;                        //中断允许
  31. }

  32. void WByte(uint input)
  33. {
  34.                          //发送启始位
  35.      uchar j=8;
  36.      newTXD=(bit)0;               //发送8位数据位
  37.      while(j--)
  38.      {
  39.          newTXD=(bit)(input&0x01);      //先传低位
  40.          input=input>>1;
  41.          Delayxms(96);  //延时 104us
  42.      }

  43.                         //发送校验位(无)

  44.                         //发送结束位
  45.     newTXD=(bit)1;
  46. }   

  47. void Sendata()
  48. {
  49.      for(i=0;i<sizeof(zhiling);i++)//外层循环,遍历数组
  50.     {
  51.           WByte(zhiling[i]);
  52.      }
  53. }

  54. /*
  55. void display()
  56. {
  57. display_graphic_16x16(1,1,0,zong16);
  58. display_graphic_16x16(1,17,0,dian16);
  59. display_graphic_16x16(1,33,0,ya16);  
  60. display_string_8x16(1,49,0,":");   //总电压
  61. display_graphic_16x16(3,1,0,zong16);
  62. display_graphic_16x16(3,17,0,dian16);
  63. display_graphic_16x16(3,33,0,liu16);  
  64. display_string_8x16(3,49,0,":");    //总电流
  65. display_graphic_16x16(5,1,0,sheng16);
  66. display_graphic_16x16(5,17,0,yu16);
  67. display_graphic_16x16(5,33,0,dian16);
  68. display_graphic_16x16(5,49,0,liang16);   
  69. display_string_8x16(5,65,0,":");     //剩余电量
  70. display_graphic_16x16(7,1,0,dian16);
  71. display_graphic_16x16(7,17,0,chi16);
  72. display_graphic_16x16(7,33,0,wen16);
  73. display_graphic_16x16(7,49,0,du16);
  74. display_string_8x16(7,65,0,":");     //电池温度
  75. }
  76. */

  77. void main()
  78. {
  79.      UartInit();
  80.      while(1)
  81.      {
  82.           Sendata();
  83.      }
  84. }
复制代码
回复

使用道具 举报

5#
ID:381738 发表于 2018-8-23 09:07 | 只看该作者
angmall 发表于 2018-8-22 15:45
给你改了一下试试。

感谢,调试了一下,看了下波形图感觉还是怪怪的,模拟串口发送程序的话是不是只要调波特率和延时就行了?附上波形图

微信图片_20180823090537.jpg (100.41 KB, 下载次数: 5)

微信图片_20180823090537.jpg
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表