找回密码
 立即注册

QQ登录

只需一步,快速开始

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

关于蓝牙模块的代码 单片机中断能实现直流电机调速吗?

[复制链接]
跳转到指定楼层
楼主
ID:202136 发表于 2017-5-31 11:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
100黑币

下面这块是读取温度的 先不管他 上面的中断能实现直流电机调速吗?  我的是普中开发板 电机应该怎么接 是接步进电机上么?能帮我调通?有红包感谢

  1. #include <reg51.h>
  2. #define  uchar  unsigned char
  3. #define  uint   unsigned int


  4. sbit PWM=P1^0;
  5. sbit DSPORT=P3^7;


  6. void Ds18b20ReadTemp();
  7. void UsartConfiguration();


  8. unsigned int temp=0;
  9. unsigned char zhuanshu=20;
  10. unsigned int cnt=0;
  11. unsigned char receiveData;
  12. unsigned int dingshi=0;
  13. unsigned char min=1;

  14. void main(void)
  15. {
  16.     unsigned int i=3000;
  17.     UsartConfiguration();
  18. while(1)
  19. {
  20. if(cnt>=3000)
  21. {
  22. cnt=0;
  23. Ds18b20ReadTemp(); //3s检测一次温度
  24. }
  25. switch(receiveData)
  26. {
  27. case 177:ET0=1;zhuanshu=20;receiveData=0;break;          // 数字1 风扇启动
  28. case 178:zhuanshu=0;while(i--);ET0=0;receiveData=0;break;        //数字2 风扇停止 由于IO口默认高电平  所以要让zhuanshu=0
  29. case 179:zhuanshu=30;break;//数字3 低挡
  30. case 180:zhuanshu=50;break;//数字4 中挡
  31. case 181:zhuanshu=70;break;// 数字5高挡
  32. case 182:                  // 数字6自动根据温度调节转速
  33. {
  34. if(temp<3200)
  35. {
  36.    zhuanshu=25;
  37. }
  38. if((temp>3200)&&(temp<3300))
  39. {
  40.    zhuanshu=30;
  41. }
  42. if((temp>3400)&&(temp<3600))
  43. {
  44.    zhuanshu=50;
  45. }
  46. if((temp>3200)&&(temp<3300))
  47. {
  48.   zhuanshu=60;
  49. }
  50. } break;                  
  51. case 183: //定时时间数字7
  52. {
  53.   static dingshi=0;
  54.    if(dingshi>=60000)  //1分钟一个基准
  55.   {
  56.      min--;
  57. dingshi=0;
  58. if(min==0)
  59. {
  60. ET0=0;
  61. zhuanshu=0;
  62. }
  63.   }
  64.   break;
  65. case 184: receiveData=0;min++;break; //增加时间  数字8
  66. case 185: receiveData=0;min--;break; //减少时间  数字9
  67. }
  68. }
  69.   }
  70. }
  71.   void UsartConfiguration()
  72. {
  73. SCON=0X50; //设置为工作方式1
  74. TMOD=0X21; //设置计数器工作方式2
  75. PCON=0X80; //波特率加倍
  76. TH0 = 0xFC;
  77. TL0 = 0x66;
  78. TH1=0XF9;     //计数器初始值设置,注意波特率是9600的
  79. TL1=0XF9;
  80. ET0=1;
  81. ES=1; //打开接收中断
  82. EA=1; //打开总中断
  83. TR1=1;
  84. TR0=1;     //打开计数器
  85. }




  86.   void Usart() interrupt 4
  87. {
  88. receiveData=SBUF; //出去接收到的数据
  89. RI = 0;           //清除接收中断标志位
  90. }


  91. void Time1(void) interrupt 1    //3 为定时器1的中断号  1 定时器0的中断号 0 外部中断1 2 外部中断2  4 串口中断
  92. {
  93.     static unsigned char timer1=0;
  94. TH0 = 0xF6;  //重新赋初值
  95. TL0 = 0x66;
  96. timer1++;
  97.     cnt++;
  98. dingshi++;
  99. if(dingshi>=60050)
  100. {
  101. dingshi=0;
  102. }
  103. if(timer1>100)  //PWM周期为100*0.5ms
  104. {
  105. timer1=0;
  106. }
  107. if(timer1 <zhuanshu)//改变zhunashu这个值可以改变直流电机的速度这个值越大转的越快
  108. {
  109.    PWM=1;
  110. }
  111. else
  112. {
  113.   PWM=0;
  114. }
  115. }


  116. void Delay1ms(uint y) //延时程序
  117. {
  118. uint x;
  119. for( ; y>0; y--)
  120. {
  121. for(x=110; x>0; x--);
  122. }
  123. }


  124. uchar Ds18b20Init() //温度的子函数
  125. {
  126. uchar i;
  127. DSPORT = 0; //将总线拉低480us~960us
  128. i = 70;
  129. while(i--);//延时642us
  130. DSPORT = 1; //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
  131. i = 0;
  132. while(DSPORT) //等待DS18B20拉低总线
  133. {
  134. i++;
  135. if(i>5)//等待>5MS
  136. {
  137. return 0;//初始化失败
  138. }
  139. Delay1ms(1);
  140. }
  141. return 1;//初始化成功
  142. }






  143. void Ds18b20WriteByte(uchar dat)
  144. {
  145. uint i, j;


  146. for(j=0; j<8; j++)
  147. {
  148. DSPORT = 0;       //每写入一位数据之前先把总线拉低1us
  149. i++;
  150. DSPORT = dat & 0x01;  //然后写入一个数据,从最低位开始
  151. i=6;
  152. while(i--); //延时68us,持续时间最少60us
  153. DSPORT = 1; //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
  154. dat >>= 1;
  155. }
  156. }






  157. uchar Ds18b20ReadByte()
  158. {
  159. uchar byte, bi;
  160. uint i, j;
  161. for(j=8; j>0; j--)
  162. {
  163. DSPORT = 0;//先将总线拉低1us
  164. i++;
  165. DSPORT = 1;//然后释放总线
  166. i++;
  167. i++;//延时6us等待数据稳定
  168. bi = DSPORT; //读取数据,从最低位开始读取
  169. /*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
  170. byte = (byte >> 1) | (bi << 7);
  171. i = 4; //读取完之后等待48us再接着读取下一个数
  172. while(i--);
  173. }
  174. return byte;
  175. }


  176. void  Ds18b20ChangTemp()
  177. {
  178. Ds18b20Init();
  179. Delay1ms(1);
  180. Ds18b20WriteByte(0xcc);//跳过ROM操作命令
  181. Ds18b20WriteByte(0x44);   //温度转换命令
  182. // Delay1ms(100);//等待转换成功,而如果你是一直刷着的话,就不用这个延时了
  183.    
  184. }




  185. void  Ds18b20ReadTempCom()
  186. {


  187. Ds18b20Init();
  188. Delay1ms(1);
  189. Ds18b20WriteByte(0xcc);//跳过ROM操作命令
  190. Ds18b20WriteByte(0xbe);//发送读取温度命令
  191. }




  192. void Ds18b20ReadTemp()
  193. {
  194.    float tp;
  195. int temp1 = 0;
  196. uchar tmh, tml;
  197. Ds18b20ChangTemp();//先写入转换命令
  198. Ds18b20ReadTempCom();//然后等待转换完后发送读取温度命令
  199. tml = Ds18b20ReadByte();//读取温度值共16位,先读低字节
  200. tmh = Ds18b20ReadByte();//再读高字节
  201. temp1 = tmh;
  202. temp1 <<= 8;
  203. temp1 |= tml;
  204.    if(temp1< 0)
  205.   {
  206. temp1=temp1-1;
  207. temp1=~temp1;
  208. tp=temp1;
  209. temp1=tp*0.0625*100+0.5;
  210.   }
  211. else
  212.   {
  213. tp=temp1;
  214. temp1=tp*0.0625*100+0.5;
  215. }


  216.   temp=temp1%10000;
  217.    
  218. }

复制代码


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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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