找回密码
 立即注册

QQ登录

只需一步,快速开始

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

瑞萨单片机计步器设计 附原理图+PCB源文件+程序代码+BOM清单

[复制链接]
跳转到指定楼层
楼主
分享瑞萨单片机计步器设计,包含程序及原理图


单片机源程序如下:
  1. /***********************************************************************************************************************
  2. * File Name    : r_main.c
  3. * Version      : V1.00
  4. * Device(s)    : R7F0C002G2DFB
  5. * Tool-Chain   : CA78K0R
  6. * Description  : This file implements main function.
  7. ***********************************************************************************************************************/

  8. /***********************************************************************************************************************
  9. * History : DD.MM.YYYY Version Description
  10. * : 31.03.2015 1.00 First Release
  11. ***********************************************************************************************************************/

  12. /***********************************************************************************************************************
  13. Includes <System Includes> , <Project Includes>
  14. ***********************************************************************************************************************/
  15. #include "r_macrodriver.h"
  16. #include "r_cgc.h"
  17. #include "r_port.h"
  18. #include "string.h"
  19. #include "r_lcd.h"
  20. #include "r_rtc.h"
  21. #include "r_timer.h"
  22. #include "r_userdefine.h"
  23. #include "r_Communication.h"
  24. #include "r_ADXL362.h"


  25. #include "pfdl.h"
  26. #include "pfdl_types.h"
  27. /***********************************************************************************************************************
  28. Global variables and functions
  29. ***********************************************************************************************************************/
  30. static uint16_t g_FontData[LCD_NUM_DATA_FONT_COUNT] = { LCD_DATA_0, LCD_DATA_1, LCD_DATA_2, LCD_DATA_3, LCD_DATA_4,
  31.                                                         LCD_DATA_5, LCD_DATA_6, LCD_DATA_7, LCD_DATA_8, LCD_DATA_9,
  32.                                                         LCD_DATA_NONE, LCD_DATA_COLON };

  33. static uint8_t g_WatchStatus;
  34. static uint8_t g_WeightStatus;
  35. static uint8_t g_Hour;
  36. static uint8_t g_Minute;
  37. uint8_t g_Weight = 0x40U;
  38. uint8_t g_Length = 0x70U;
  39. uint16_t g_Step;
  40. uint16_t g_Step_last;

  41. uint8_t Count_flag = 0U;

  42. uint8_t g_Step_temp1[2] = {0x00,0x00};
  43. uint8_t g_Step_temp2[2] = {0x00,0x00};

  44. uint16_t g_Calorie;
  45. uint8_t set_flag = 0U;

  46. uint8_t step1_flag = 0U;
  47. uint8_t step2_flag = 0U;

  48. uint8_t switch_status      = 0U;
  49. uint8_t status             = 0U;
  50. short         xAxis        = 0U;
  51. short         yAxis        = 0U;
  52. short         zAxis        = 0U;
  53. float         temperature  = 0U;
  54. pfdl_u16 StartAddrIndex    = 0U;

  55. #define FALSE    0
  56. #define TRUE     1
  57. #define BLOCKBYTES 1024

  58. void R_MAIN_UserInit(void);
  59. pfdl_u08 Write_DataFlash(void);
  60. pfdl_u08 Read_DataFlash(void);
  61. pfdl_u08 Erase_DataFlash(pfdl_u16 block);
  62. /***********************************************************************************************************************
  63. * Function Name: main
  64. * Description  : This function implements main function.
  65. * Arguments    : None
  66. * Return Value : None
  67. ***********************************************************************************************************************/
  68. void main(void)
  69. {
  70.     uint8_t set_count = 0U;
  71.     uint8_t count = 0U;
  72.     rtc_counter_value_t time_data;
  73.     g_Hour = 0x00U;
  74.     g_Minute = 0x00U;
  75.    
  76.     R_MAIN_UserInit();

  77.     Read_DataFlash();
  78.     g_Step_last = (((short)g_Step_temp2[1] << 8) + g_Step_temp2[0]);
  79.     if(0xFFFFU == g_Step_last)
  80.         g_Step_last = 0U;

  81.     while(1U)
  82.     {
  83.         switch_status = R_MAIN_Get_SwitchStatus();

  84.         if((0x23U == HOUR) && (0x59U == MIN) && (0x59U == SEC)&& (1U == RIFG))
  85.         {
  86.             DI();
  87.             RIFG = 0U;
  88.             g_Step_temp1[0] = (uint8_t)(g_Step & 0x00ff);
  89.             g_Step_temp1[1] = (uint8_t)((g_Step >> 8) & 0x00ff);

  90.             Write_DataFlash();
  91.             g_Step = 0U;
  92.             g_Calorie = 0U;
  93.             Read_DataFlash();
  94.             g_Step_last = (((short)g_Step_temp2[1] << 8) + g_Step_temp2[0]);
  95.             EI();
  96.         }

  97.         if(0U == set_flag)
  98.         {
  99.             switch(count)
  100.             {
  101.                 case 0:
  102.                 {
  103.                     R_RTC_Get_CounterValue(&time_data);
  104.                     g_Hour = time_data.hour;
  105.                     g_Minute = time_data.min;
  106.                     /* ---- Time displays on LCD ---- */
  107.                     R_MAIN_LcdDisplayNormal();
  108.                 }
  109.                 break;
  110.                
  111.                 case 1:
  112.                 {
  113.                     /* ---- Steps displays on LCD ---- */
  114.                     R_MAIN_LcdStepDisplayNormal();
  115.                 }
  116.                 break;
  117.                     
  118.                 case 2:
  119.                 {
  120.                     
  121.                     /* ---- Carlorie displays on LCD ---- */
  122.                     R_MAIN_LcdCalorieDisplayNormal();
  123.                 }
  124.                 break;
  125.                
  126.                 case 3:
  127.                 {
  128.                     /* ---- Last Steps displays on LCD ---- */
  129.                     R_MAIN_LcdLastStepDisplayNormal();
  130.                 }
  131.                 break;
  132.                 default:
  133.                 break;
  134.             }
  135.             if(switch_status == TRANS_SWITCH_ON)
  136.             {
  137.                 count++;
  138.                 if(4U == count)
  139.                 count = 0U;
  140.             }
  141.         }

  142.         else if(1U == set_flag)
  143.         {
  144.             switch(set_count)
  145.             {
  146.                 case 0 :
  147.                 {
  148.                     R_MAIN_LcdHourBlink();
  149.                     if(UP_SWITCH_ON == switch_status)
  150.                     {
  151.                         g_Hour = adbcdb(g_Hour, 1U);
  152.                         /* ---- When hour data is 0x24 ---- */
  153.                         if (0x24U == g_Hour)
  154.                         {
  155.                             g_Hour = 0x00U;
  156.                         }
  157.                         time_data.hour = g_Hour;
  158.                         R_RTC_Set_CounterValue(time_data);
  159.                     }
  160.                     else if(DOWN_SWITCH_ON == switch_status)
  161.                     {
  162.                         g_Hour = sbbcdb(g_Hour, 1U);
  163.                         if (0x99U == g_Hour)
  164.                         {
  165.                             g_Hour = 0x23U;
  166.                         }
  167.                         time_data.hour = g_Hour;
  168.                         R_RTC_Set_CounterValue(time_data);
  169.                     }
  170.                 }
  171.                 break;
  172.                
  173.                 case 1 :
  174.                 {
  175.                     R_MAIN_LcdMinuteBlink();
  176.                     if(UP_SWITCH_ON == switch_status)
  177.                     {
  178.                         g_Minute = adbcdb(g_Minute, 1U);
  179.                         /* ---- When hour data is 0x24 ---- */
  180.                         if (0x60U == g_Minute)
  181.                         {
  182.                             g_Minute = 0x00U;
  183.                         }
  184.                         time_data.min = g_Minute;
  185.                         R_RTC_Set_CounterValue(time_data);
  186.                     }
  187.                     else if(DOWN_SWITCH_ON == switch_status)
  188.                     {
  189.                         g_Minute = sbbcdb(g_Minute, 1U);
  190.                         /* ---- When hour data is 0x24 ---- */
  191.                         if (0x99U == g_Minute)
  192.                         {
  193.                             g_Minute = 0x59U;
  194.                         }
  195.                         time_data.min = g_Minute;
  196.                         R_RTC_Set_CounterValue(time_data);
  197.                     }
  198.                     
  199.                 }
  200.                 break;
  201.                
  202.                 case 2 :
  203.                 {
  204.                     R_MAIN_LcdWeightBlink();
  205.                     if(UP_SWITCH_ON == switch_status)
  206.                     {
  207.                         g_Weight = adbcdb(g_Weight, 1U);
  208.                         /* ---- When hour data is 0x24 ---- */
  209.                         if (0x9AU == g_Weight)
  210.                         {
  211.                             g_Weight = 0x00U;
  212.                         }
  213.                     }
  214.                     else if(DOWN_SWITCH_ON == switch_status)
  215.                     {
  216.                         /* ---- When hour data is 0x24 ---- */
  217.                         if (0x00U == g_Weight)
  218.                         {
  219.                             g_Weight = 0x9AU;
  220.                         }
  221.                         g_Weight = sbbcdb(g_Weight, 1U);
  222.                     }
  223.                 }
  224.                 break;
  225.                
  226.                 case 3 :
  227.                 {
  228.                     R_MAIN_LcdLengthBlink();
  229.                     if(UP_SWITCH_ON == switch_status)
  230.                     {
  231.                         g_Length = adbcdb(g_Length, 1U);
  232.                         /* ---- When hour data is 0x24 ---- */
  233.                         if (0x9AU == g_Length)
  234.                         {
  235.                             g_Length = 0x00U;
  236.                         }
  237.                     }
  238.                     else if(DOWN_SWITCH_ON == switch_status)
  239.                     {
  240.                         /* ---- When hour data is 0x24 ---- */
  241.                         if (0x00U == g_Length)
  242.                         {
  243.                             g_Length = 0x9AU;
  244.                         
  245.                         }
  246.                         g_Length = sbbcdb(g_Length, 1U);
  247.                     }
  248.                 }
  249.                 break;
  250.                
  251.                 default:
  252.                 break;
  253.             }
  254.             if(TRANS_SWITCH_ON == switch_status)
  255.             {
  256.                 set_count++;
  257.                 if(4U == set_count)
  258.                 {
  259.                      set_count = 0U;
  260.                      set_flag = 0U;
  261.                 }
  262.             }
  263.         }
  264.     }
  265. }
  266. /***********************************************************************************************************************
  267. End of function main
  268. ***********************************************************************************************************************/

  269. /***********************************************************************************************************************
  270. * Function Name: R_MAIN_UserInit
  271. * Description  : This function adds user code before implementing main function.
  272. * Arguments    : None
  273. * Return Value : None
  274. ***********************************************************************************************************************/
  275. void R_MAIN_UserInit(void)
  276. {
  277.     EI();
  278.    
  279.     R_LCD_Start();
  280.     /* ---- Initialize display ---- */
  281.     memset(LCD_POSITION_MINUTE_LOW, LCD_DATA_NONE, 9U);

  282.     g_WatchStatus = WATCH_DISPLAY;

  283.     R_RTC_Set_ConstPeriodInterruptOn(ONESEC);
  284.     R_RTC_Start();
  285.     R_TAU0_Channel0_Start();
  286.     R_TAU0_Channel1_Start();

  287.     /* Init device ADXL362. */
  288.     ADXL362_Init();
  289.     /* Put the device in standby mode. */
  290.     ADXL362_SetPowerMode(0U);
  291.     /* Set accelerometer's output data rate to: 12.5 Hz. */
  292.     ADXL362_SetOutputRate(ADXL362_ODR_12_5_HZ);
  293.     /* Setup the activity and inactivity detection. */
  294.     ADXL362_SetRegisterValue(
  295.                   ADXL362_ACT_INACT_CTL_LINKLOOP(ADXL362_MODE_LINK),
  296.                   ADXL362_REG_ACT_INACT_CTL,
  297.                   1U);
  298.     ADXL362_SetupActivityDetection(1U, 30U, 1U);
  299.     ADXL362_SetupInactivityDetection(1U, 700U, 25U);
  300.     /* Start the measurement process. */
  301.     ADXL362_SetPowerMode(1U);
  302.     /* Clear ACT and INACT bits by reading the Status Register. */
  303.     ADXL362_GetRegisterValue(&status,
  304.                              ADXL362_REG_STATUS,
  305.                              1U);
  306. }
  307. /***********************************************************************************************************************
  308. End of function R_MAIN_UserInit
  309. ***********************************************************************************************************************/

  310. /***********************************************************************************************************************
  311. * Function Name: R_MAIN_GetSwitchStatus
  312. * Description  : This function get switch status.
  313. * Arguments    : None
  314. * Return Value : switch_status -The kind of pushed switch
  315. ***********************************************************************************************************************/
  316. uint8_t R_MAIN_Get_SwitchStatus(void)
  317. {
  318.     uint8_t set_switch;
  319.     uint8_t up_switch;
  320.     uint8_t down_switch;
  321.     uint8_t trans_switch;

  322.     uint8_t switch_status = SWITCH_ALL_OFF;

  323.     set_switch = P2.1;
  324.     up_switch = P2.0;
  325.     down_switch = P14.4;
  326.     trans_switch = P14.3;

  327.     /* ---- Set switch status ---- */
  328.     if ((0U == set_switch) && (1U == up_switch) && (1U == down_switch) && (1U == trans_switch))
  329.     {
  330.         R_MAIN_Delay1ms(10U);
  331.         set_switch = P2.1;
  332.         up_switch = P2.0;
  333.         down_switch = P14.4;
  334.         trans_switch = P14.3;
  335.         if((0U == set_switch) && (1U == up_switch) && (1U == down_switch) && (1U == trans_switch))
  336.         {
  337.             while(0U == set_switch)
  338.             {
  339.                 set_switch = P2.1;
  340.                 set_flag  = 1U;
  341.             }
  342.             switch_status = SET_SWITCH_ON;
  343.         }
  344.     }
  345.     else if ((1U == set_switch) && (0U == up_switch) && (1U == down_switch) && (1U == trans_switch))
  346.     {
  347.         R_MAIN_Delay1ms(10U);
  348.         set_switch = P2.1;
  349.         up_switch = P2.0;
  350.         down_switch = P14.4;
  351.         trans_switch = P14.3;
  352.         if((1U == set_switch) && (0U == up_switch) && (1U == down_switch) && (1U == trans_switch))
  353.         {
  354.             while(0U == up_switch)
  355.             {
  356.                 up_switch = P2.0;
  357.             }
  358.             switch_status = UP_SWITCH_ON;
  359.         }
  360.     }
  361.     else if ((1U == set_switch) && (1U == up_switch) && (0U == down_switch) && (1U == trans_switch))
  362.     {
  363.         R_MAIN_Delay1ms(10U);
  364.         set_switch = P2.1;
  365.         up_switch = P2.0;
  366.         down_switch = P14.4;
  367.         trans_switch = P14.3;
  368.         if((1U == set_switch) && (1U == up_switch) && (0U == down_switch) && (1U == trans_switch))
  369.         {
  370.             while(0U == down_switch)
  371.             {
  372.                 down_switch = P14.4;
  373.             }
  374.             switch_status = DOWN_SWITCH_ON;
  375.         }
  376.     }
  377.     else if ((1U == set_switch) && (1U == up_switch) && (1U == down_switch) && (0U == trans_switch))
  378.     {
  379.         R_MAIN_Delay1ms(10U);
  380.         set_switch = P2.1;
  381.         up_switch = P2.0;
  382.         down_switch = P14.4;
  383.         trans_switch = P14.3;
  384.         if((1U == set_switch) && (1U == up_switch) && (1U == down_switch) && (0U == trans_switch))
  385.         {
  386.             while(0U == trans_switch)
  387.             {
  388.                 trans_switch = P14.3;
  389.             }
  390.             switch_status = TRANS_SWITCH_ON;
  391.         }
  392.     }
  393.     else
  394.     {
  395.         /* Do Nothing */
  396.     }
  397.     return (switch_status);
  398. }
  399. /***********************************************************************************************************************
  400. End of function R_MAIN_GetSwitchStatus
  401. ***********************************************************************************************************************/

  402. /***********************************************************************************************************************
  403. * Function Name: R_MAIN_LcdHourBlink
  404. * Description  : This function hour blink data setup.
  405. * Arguments    : None
  406. * Return Value : None
  407. ***********************************************************************************************************************/
  408. void R_MAIN_LcdHourBlink(void)
  409. ……………………

  410. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
电子计步器设计方案,附原理图+PCB源文件+源代码+BOM清单.zip (3.92 MB, 下载次数: 61)
r7f0c002_pedometer_brochure.pdf (287.65 KB, 下载次数: 27)



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

沙发
ID:92810 发表于 2019-5-27 10:41 | 只看该作者
谢谢楼主分享了,学习学习了
回复

使用道具 举报

板凳
ID:953542 发表于 2021-7-15 18:10 | 只看该作者
很不错,值得推荐的资料
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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