久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2227|回復: 1
打印 上一主題 下一主題
收起左側

stm32f4簡易電子秤程序

[復制鏈接]
跳轉到指定樓層
樓主
ID:374354 發表于 2018-7-18 20:04 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
簡易電子秤程序,在此分享,歡迎下載

單片機源程序如下:
  1. #include "stm32f4xx.h"
  2. #include "stm32f4xx_conf.h"
  3. #include "stm32f4xx_it.h"
  4. #include <stdio.h>       //C語言頭文件
  5. #include "LCD.h"
  6. #include "math.h"        //C語言的頭文件
  7. #include "WAVEDAT.h"  

  8. #define KEY_RAM        (*((volatile unsigned short *) 0x60070000))     //外部地址是從0x60000000開始的




  9. GPIO_InitTypeDef           GPIO_InitStructure;
  10. DAC_InitTypeDef            DAC_InitStructure;
  11. TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;
  12. NVIC_InitTypeDef           NVIC_InitStructure;

  13. ADC_InitTypeDef            ADC_InitStructure;
  14. ADC_CommonInitTypeDef      ADC_CommonInitStructure;


  15. u16 volatile i,j;
  16. u8  posbit=0x00;
  17. u8  posbit1=0x00;
  18. u8  posbit2=0x00;
  19. u16 DACDAT;
  20. u8 keysign=0;
  21. u8 keycode;
  22. u8 duty=0x02;
  23. //u16 count;
  24. u8  sindat[256];
  25. char temp[3];
  26. u8 k;
  27. u16 timer,tdata;

  28. u8 RANGE;
  29. //u16 adc_value;          //存放A/D轉換值
  30. //u8  ADCSIGN;            //A/D轉換結束標志
  31. //*********************************
  32. u32 price=0;//單價
  33. u32 count=0;//計價
  34. u32 sum=0;//累計
  35. u32 weight=0;//質量
  36. u32 weight1=0;
  37. u32 tare=0;//皮重
  38. u8 flag_int=1;//整數小數輸入標志,1=整數
  39. u8 flag_price=0;//單價輸入標志,1=正在輸入單價
  40. u8 num_num=0;
  41. u32 number=0;
  42. u8 number1=0;
  43. u8 number2=0;
  44. u32 value1[5];
  45. //*********************************
  46. void FSMC_init(void);
  47. void GPIO_Configuration(void);
  48. void EXTI_init(void);
  49. void TIM1_init(void);
  50. void TIM2_init(void);
  51. void TIM1INT_init(void);
  52. void TIM2INT_init(void);
  53. void DAC_init(void);
  54. //void ADC_init(void);                    //ADC初始化
  55. void PE0Tog(void);
  56. void PE3Tog(void);
  57. void PE2Tog(void);
  58. void SysClk_Init(u32 clock);
  59. void delay_ms(volatile u16 time);

  60. //*********************************
  61. //          IIC 初始化
  62. //*********************************
  63. void I2C_Configuration(void)
  64. {
  65.   I2C_InitTypeDef  I2C_InitStructure;
  66.   GPIO_InitTypeDef  GPIO_InitStructure;

  67.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
  68.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);

  69.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
  70.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);

  71.   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_8 | GPIO_Pin_9;
  72.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  73.   GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  74.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  75.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  76.   GPIO_Init(GPIOB, &GPIO_InitStructure);

  77.   I2C_DeInit(I2C1);
  78.   I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  79.   I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  80.   I2C_InitStructure.I2C_OwnAddress1 = 0x91;  //地址
  81.   I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;  //ack enable
  82.   I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  83.   I2C_InitStructure.I2C_ClockSpeed = 100000;  //100k

  84.   I2C_Cmd(I2C1, ENABLE);
  85.   I2C_Init(I2C1, &I2C_InitStructure);

  86.   //I2C_AcknowledgeConfig(I2C1, ENABLE);
  87. }
  88. //*********************************
  89. //          IIC Read
  90. //*********************************
  91. char value[2];
  92. void I2C_Read(void)
  93. {  
  94.     /*
  95.   while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))
  96.   {;}
  97.   I2C_AcknowledgeConfig(I2C1, ENABLE);

  98.   I2C_GenerateSTART(I2C1, ENABLE);
  99.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
  100.   {;}
  101.   I2C_Send7bitAddress(I2C1,  0x91, I2C_Direction_Transmitter);
  102.   while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
  103.   {;}
  104.   I2C_SendData(I2C1, addr);
  105.   while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  106.   {;}
  107. */
  108.   I2C_GenerateSTART(I2C1, ENABLE);
  109.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
  110.   {;}

  111.   I2C_Send7bitAddress(I2C1, 0x91, I2C_Direction_Receiver);
  112.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
  113.   {;}

  114.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))  // EV7
  115.   {;}
  116.   value[0] = I2C_ReceiveData(I2C1);   

  117.   I2C_AcknowledgeConfig(I2C1, DISABLE);
  118.   I2C_GenerateSTOP(I2C1, ENABLE);   

  119.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))  // EV7  
  120.   {;}
  121.   value[1] = I2C_ReceiveData(I2C1);
  122.   //cal_data = value[0]<<8 | value[1];

  123.   //printf("buf[0] = %x,buf[1] = %x,%d,%d\r\n",buf[0],buf[1],(buf[0]<<8|buf[2]),cal_data);   
  124.   // Decrement the read bytes counter
  125.   I2C_AcknowledgeConfig(I2C1, ENABLE);
  126. }
  127. //*********************************
  128. //          四位整數兩位小數顯示
  129. //*********************************
  130. void printnum(u16 x,u16 y,u32 num,u16 color)
  131. {
  132.     u32 temp;
  133.     temp=num/100;
  134.     LCD_ShowNumBig_L(x+48,x-16,y,temp,color);
  135.    
  136.     LCD_ShowCharBig(x+48,y,'.',color);
  137.    
  138.     temp=num/10%10;
  139.     LCD_ShowCharBig(x+64,y,temp+'0',color);
  140.     temp=num%10;
  141.     LCD_ShowCharBig(x+80,y,temp+'0',color);
  142. }
  143. //*********************************
  144. //          質量刷新
  145. //*********************************
  146. u32 weight2=0;
  147. void printweight(void)
  148. {
  149.     printnum(120,190,weight2,BLACK);
  150.     printnum(120,190,weight-tare,WHITE);
  151.     weight2=weight-tare;
  152. }
  153. //*********************************
  154. //          累計刷新
  155. //*********************************
  156. u32 sum2=0;
  157. void printsum(void)
  158. {
  159.     printnum(350,80,sum2,BLACK);
  160.     printnum(350,80,sum,WHITE);
  161.     sum2=sum;
  162. }
  163. //*********************************
  164. //          單價刷新
  165. //*********************************
  166. u32 price2=0;
  167. void printprice(void)
  168. {
  169.     printnum(120,80,price2,BLACK);
  170.     printnum(120,80,price,WHITE);
  171.     price2=price;
  172. }
  173. //*********************************
  174. //          計價刷新
  175. //*********************************
  176. u32 count2=0;
  177. void printcount(void)
  178. {
  179.     printnum(350,190,count2,BLACK);
  180.     printnum(350,190,count,WHITE);
  181.     count2=count;
  182. }
  183. //*********************************
  184. //          按鍵處理
  185. //*********************************
  186. u16 temp_price_high=0;
  187. u16 temp_price_low=0;
  188. void keycontrol(void)
  189. {
  190.     if(keysign==1)
  191.     {
  192.         keysign=0;
  193.         if(keycode==0x07)//單價
  194.         {
  195.             price=0;
  196.             printprice();
  197.             temp_price_high=0;
  198.             temp_price_low=0;
  199.             flag_price=1;
  200.             flag_int=1;
  201.             num_num=0;
  202.         }
  203.         else if(keycode==0x03&&flag_price==1)//確定
  204.         {
  205.             flag_price=0;
  206.             price=temp_price_high*100+temp_price_low;
  207.             printcount();
  208.         }
  209.         else if(keycode==0x0f)//去皮
  210.         {
  211.             tare=weight;
  212.         }
  213.         else if(keycode==0x0e)//復位
  214.         {
  215.             price=0;
  216.             temp_price_high=0;
  217.             temp_price_low=0;
  218.             count=0;
  219.             sum=0;
  220.             flag_price=0;
  221.             flag_int=1;
  222.             tare=0;
  223.             num_num=0;
  224.             printweight();
  225.             printsum();
  226.             printprice();
  227.             printcount();
  228.         }
  229.         else if(keycode==0x0d&&flag_price==1)//小數點
  230.         {
  231.             flag_int=0;
  232.             num_num=0;
  233.         }
  234.         else if(keycode==0x0b)//累加
  235.         {
  236.             sum=sum+count;
  237.             printsum();
  238.         }
  239.         else if(flag_price==1)//數字按鍵
  240.         {
  241.             u16 temp=0;
  242.             switch(keycode)
  243.             {
  244.                 case 0x0c:
  245.                 {
  246.                     temp=0;
  247.                     break;
  248.                 }
  249.                 case 0x08:
  250.                 {
  251.                     temp=1;
  252.                     break;
  253.                 }
  254.                 case 0x09:
  255.                 {
  256.                     temp=2;
  257.                     break;
  258.                 }
  259.                 case 0x0a:
  260.                 {
  261.                     temp=3;
  262.                     break;
  263.                 }
  264.                 case 0x04:
  265.                 {
  266.                     temp=4;
  267.                     break;
  268.                 }
  269.                 case 0x05:
  270.                 {
  271.                     temp=5;
  272.                     break;
  273.                 }
  274.                 case 0x06:
  275.                 {
  276.                     temp=6;
  277.                     break;
  278.                 }
  279.                 case 0x00:
  280.                 {
  281.                     temp=7;
  282.                     break;
  283.                 }
  284.                 case 0x01:
  285.                 {
  286.                     temp=8;
  287.                     break;
  288.                 }
  289.                 case 0x02:
  290.                 {
  291.                     temp=9;
  292.                     break;
  293.                 }
  294.             }
  295.             if(flag_int==1)//如果輸入整數
  296.             {
  297.                 num_num++;
  298.                 if(num_num<=3)
  299.                 {
  300.                     temp_price_high=temp_price_high*10+temp;
  301.                 }
  302.             }
  303.             else//輸入單價小數
  304.             {
  305.                 num_num++;
  306.                 if(num_num==1)
  307.                 {
  308.                     temp_price_low=temp*10;
  309.                 }
  310.                 if(num_num==2)
  311.                 {
  312.                     temp_price_low=temp_price_low+temp;
  313.                 }
  314.             }
  315.             price=temp_price_high*100+temp_price_low;
  316.             printprice();
  317.         }
  318.         
  319.     }

  320.    
  321. }
  322. //*********************************
  323. //          main
  324. //*********************************
  325. int main(void)
  326. {
  327.     FSMC_init();
  328.     LCD_Init();                                                   //液晶初始化
  329.                                                 //外部中斷初始化
  330.     EXTI_ClearITPendingBit(EXTI_Line1);
  331.     GPIO_Configuration();                       //I/O初始化
  332.     TIM1_init();
  333.     TIM2_init();
  334.     DAC_init();
  335.     //ADC_init();
  336.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  337.     EXTI_init();     
  338.     TIM1INT_init();
  339.     TIM2INT_init();
  340.     RCC_MCO1Config(RCC_MCO1Source_HSE,RCC_MCO1Div_4);            //MCO選擇HSI,4分頻,因此PA8輸出3MHz方波
  341.     SysClk_Init(1000);
  342.    
  343.     LCD_ShowChineseStringBig(160,270,8,5,YELLOW);               
  344.    
  345.     LCD_ShowChineseStringBig(40,190,13,2,YELLOW);               
  346.     LCD_ShowChineseStringBig(270,190,19,2,YELLOW);               
  347.     LCD_ShowChineseStringBig(40,80,17,2,YELLOW);               
  348.     LCD_ShowChineseStringBig(270,80,15,2,YELLOW);               
  349.    
  350.     TFTLED=0x01;                                                //開啟背光
  351.     keysign=0;
  352.    
  353.     I2C_Configuration();
  354.    
  355.     printweight();//顯示初始值
  356.     printsum();
  357.     printprice();
  358.     printcount();

  359.     while(1)
  360.     {
  361.        keycontrol();
  362.        if(number1==1)
  363.        {
  364.            number1=0;
  365.            /*
  366.            I2C_Read();
  367.            weight=value[0]*256+value[1];
  368.            LCD_ShowNumBig(5,20,weight,WHITE);
  369.            */
  370.            if(number2<5)
  371.            {
  372.                I2C_Read();
  373.                value1[number2]=value[0]*256+value[1];//高八位低八位合成
  374.                number2++;
  375.            }
  376.            else
  377.            {
  378.                number2=0;
  379.                weight=0;
  380.                for(u8 i=0;i<5;i++)
  381.                {
  382.                    weight+=value1[number2];
  383.                }
  384.                weight=weight/5;//五組數據取平均
  385.                //LCD_ShowNumBig(5,20,weight,WHITE);
  386.                weight=weight*15135-140000000;//質量換算
  387.                weight=weight/1000;
  388.                //LCD_ShowNumBig(205,20,weight,WHITE);
  389.                weight=618;
  390.                printweight();
  391.                count=price*(weight-tare)/100;
  392.                printcount();
  393.            }
  394.        }
  395.     }
  396. }




  397. //----------------------------------------------------------------------------
  398. //定時器1中斷服務程序
  399. //----------------------------------------------------------------------------

  400. void TIM1_UP_TIM10_IRQHandler(void)
  401. {
  402. if ( TIM_GetITStatus(TIM1,TIM_IT_Update) != RESET ) //是否發生中斷
  403.   {
  404.    
  405.     TIM_ClearITPendingBit(TIM1, TIM_IT_Update);    //清除中斷待處理位
  406.       PE2Tog();

  407.    
  408.    }
  409. }  





  410. //----------------------------------------------------------------------------
  411. //定時器2中斷服務程序
  412. //----------------------------------------------------------------------------

  413. void TIM2_IRQHandler(void)    //TIM2中斷
  414. {
  415.    if(TIM_GetITStatus (TIM2,TIM_IT_Update)!=RESET)
  416.    {
  417.     TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
  418.     PE3Tog();
  419.     number++;
  420.     if(number>100)
  421.     {
  422.         number=0;
  423.         number1=1;
  424.     }
  425.   }

  426. }  

  427. void EXTI0_IRQHandler(void)        //鍵盤中斷
  428. {
  429.         keycode = KEY_RAM;
  430.         keycode &= 0x0f;
  431.         keysign = 1;
  432.       
  433.         EXTI_ClearITPendingBit(EXTI_Line0);
  434.         
  435. }

  436. /*
  437. void ADC_IRQHandler(void)
  438. {
  439.   count++;
  440.   adc_value = ADC_GetConversionValue(ADC1);
  441.   ADC_ClearITPendingBit(ADC1,ADC_IT_EOC);
  442.   if (count==500)
  443.   {
  444.       ADCSIGN=1;
  445.       count=0;
  446.   }
  447. }*/



  448. void SysTick_Handler(void)
  449. {
  450.   
  451.     PE0Tog();
  452. }  

  453. void FSMC_init(void)
  454. {
  455.         GPIO_InitTypeDef GPIO_InitStructure;
  456.         FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
  457.         FSMC_NORSRAMTimingInitTypeDef FSMC_NORSRAMTimingInitStructure;

  458.         RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE); //使能FSMC時鐘
  459.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE, ENABLE);

  460.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //復用推挽輸出
  461.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  462.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  463.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  464.         //PORTD復用推挽輸出
  465.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FSMC);    //AD2
  466.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FSMC);    //AD3
  467.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_FSMC);    //NOE
  468.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_FSMC);    //NWE
  469.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_FSMC);    //AD13
  470.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_FSMC);    //AD14
  471.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_FSMC);   //AD15
  472.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource11, GPIO_AF_FSMC);   //A16
  473.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_FSMC);   //A17
  474.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_FSMC);   //A18
  475.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_FSMC);   //AD0
  476.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_FSMC);   //AD1
  477.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 |
  478.                                                                 GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_9 |
  479.                                                                 GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 |
  480.                                                                 GPIO_Pin_13| GPIO_Pin_14 | GPIO_Pin_15;
  481.         GPIO_Init(GPIOD, &GPIO_InitStructure);
  482.        
  483.         //PORTE復用推挽輸出
  484.         GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_FSMC);    //AD4
  485.         GPIO_PinAFConfig(GPIOE, GPIO_PinSource8, GPIO_AF_FSMC);    //AD5
  486.         GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_FSMC);    //AD6
  487.         GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_FSMC);   //AD7
  488.         GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_FSMC);   //AD8
  489.         GPIO_PinAFConfig(GPIOE, GPIO_PinSource12, GPIO_AF_FSMC);   //AD9
  490.         GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_FSMC);   //AD10
  491.         GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_FSMC);   //AD11
  492.         GPIO_PinAFConfig(GPIOE, GPIO_PinSource15, GPIO_AF_FSMC);   //AD12
  493.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 |
  494.                                                                 GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 |
  495.                                                                 GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  496.         GPIO_Init(GPIOE, &GPIO_InitStructure);

  497.         //PORTB復用推挽輸出
  498.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_FSMC);   //NDAV
  499.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  500.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  501.        
  502.         FSMC_NORSRAMTimingInitStructure.FSMC_AddressSetupTime = 7;
  503.         FSMC_NORSRAMTimingInitStructure.FSMC_AddressHoldTime = 4;
  504.         FSMC_NORSRAMTimingInitStructure.FSMC_DataSetupTime = 10;
  505.         FSMC_NORSRAMTimingInitStructure.FSMC_BusTurnAroundDuration = 0;
  506.         FSMC_NORSRAMTimingInitStructure.FSMC_CLKDivision = 0;
  507.         FSMC_NORSRAMTimingInitStructure.FSMC_DataLatency = 0;
  508.         FSMC_NORSRAMTimingInitStructure.FSMC_AccessMode = FSMC_AccessMode_B;

  509.         FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
  510.         FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Enable;
  511.         FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR;
  512.         FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  513.         FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  514.         FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  515.         FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  516.         FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  517.         FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  518.         FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  519.         FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
  520.         FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  521.         FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  522.         FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &FSMC_NORSRAMTimingInitStructure;

  523.         FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  524.         /* Enable FSMC Bank1_SRAM Bank */
  525.         FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
  526. }


  527. void GPIO_Configuration(void)
  528. {
  529.       RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);      //使能GPIOA時鐘
  530.       RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);      //使能GPIOC時鐘
  531.       RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);      //使能GPIOE時鐘
  532.       
  533.       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;             //PE0推拉輸出
  534.       GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  535.       GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  536.       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  537.       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  538.       GPIO_Init(GPIOE, &GPIO_InitStructure);     
  539.       
  540.       
  541.       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;             //PE3和PE2推拉輸出輸出
  542.       GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  543.       GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  544.       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  545.       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_2;    //
  546.       GPIO_Init(GPIOE, &GPIO_InitStructure);  
  547.       
  548.       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;             //PE3和PE2推拉輸出輸出
  549.       GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  550.       GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  551.       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  552.       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_0;    //
  553.       GPIO_Init(GPIOC, &GPIO_InitStructure);  
  554.       
  555.       
  556.       GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_MCO);
  557.       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;             //PA8推拉輸出輸出
  558.       GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  559.       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  560.       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;                 
  561.       GPIO_Init(GPIOA, &GPIO_InitStructure);  
  562.         
  563.       
  564. }


  565. void EXTI_init(void)
  566. {
  567.        
  568.   
  569.       EXTI_InitTypeDef EXTI_InitStructure;
  570.       NVIC_InitTypeDef NVIC_InitStructure;
  571.       GPIO_InitTypeDef GPIO_InitStructure;
  572.      
  573.       RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //Enable GPIOB clock
  574.       RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);// Enable SYSCFG clock

  575.       
  576.       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  577.       GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  578.       GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  579.       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  580.       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  581.       GPIO_Init(GPIOB, &GPIO_InitStructure);
  582.       
  583.       
  584.       SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource0);/* Connect EXTI Line0 to PB pin */
  585.       
  586.       EXTI_InitStructure.EXTI_Line = EXTI_Line0;/* Configure EXTI Line0 */
  587.       EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  588.       EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  589.       EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  590.       EXTI_Init(&EXTI_InitStructure);
  591.       EXTI_ClearFlag(EXTI_Line0);
  592.       /* Enable and set EXTI Line0 Interrupt to the lowest priority */
  593.       NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
  594.       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
  595.       NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
  596.       NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  597.       NVIC_Init(&NVIC_InitStructure);
  598.   
  599.         keycode = 0;
  600.         keysign = 0;
  601. }

  602. void TIM1_init(void)
  603. {
  604.       /* TIM1 Configuration */
  605.      /*定時時間TOUT=(124+1)*(167+1)/168=125us*/
  606.       RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);           //使能TIM1
  607.       TIM_DeInit(TIM1);                                             //復位定時器1
  608.       TIM_TimeBaseStructure.TIM_Period=124;                         //設置自動重裝載寄存器周期的值
  609.       TIM_TimeBaseStructure.TIM_Prescaler=167;                       //設置時鐘頻率除數的預分頻值
  610.       TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;         //設置時鐘分割TIM_CKD_DIV1=0x0000,
  611.       TIM_TimeBaseStructure.TIM_RepetitionCounter=0x00;             //設置RCR寄存器值(這個只有高級定時器中有)
  612.       TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;     //TIIM向上計數
  613.       TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);                //初始化TIM3
  614.       TIM_Cmd(TIM1,ENABLE);                                         //開啟TIM1計數
  615.       TIM_ClearFlag(TIM1, TIM_FLAG_Update);                         //清溢出標志
  616. }

  617. void TIM2_init(void)
  618. {
  619.       /*我估計SYSCLK為168MHz,AHB的預分頻器為1,APB1預分頻器為4,因此APB1的時鐘頻率為42MHz,TIM2的時鐘為84MHz*/
  620.       /* 定時時間TOUT=(610+1)*(167+1)/84=1000us  */
  621.       RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);        //使能TIM2
  622.       TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);               //TIM2 Configuration
  623.       TIM_TimeBaseStructure.TIM_Period =499;                       //  
  624.       TIM_TimeBaseStructure.TIM_Prescaler = 167;   
  625.       TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;   
  626.       TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  627.       TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  628.       /* TIM2 TRGO selection */
  629.       TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);      //更新事件被選為觸發輸入
  630.   
  631.       /* TIM2 enable counter */
  632.       TIM_Cmd(TIM2, ENABLE);
  633. }
  634. void TIM1INT_init(void)
  635. /* Enable the TIM1 global Interrupt */
  636. {
  637.       NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
  638.       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  639.       NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  640.       NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  641.       NVIC_Init(&NVIC_InitStructure);
  642.       TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);                      //允許溢出中斷
  643. }




  644. void TIM2INT_init(void)
  645. /* Enable the TIM2 global Interrupt */
  646. {
  647.       NVIC_InitStructure.NVIC_IRQChannel =  TIM2_IRQn;
  648.       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  649.       NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  650.       NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  651.       NVIC_Init(&NVIC_InitStructure);
  652.       TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);                      //允許溢出中斷
  653. }





  654. void DAC_init(void)
  655. {
  656.       RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);        //使能DAC時鐘
  657.       GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4 | GPIO_Pin_5;     //PA.4 DAC1輸出;PA.5DAC2輸出
  658.       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  659.       GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  660.    
  661.       GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  662.       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  663.       GPIO_Init(GPIOA,&GPIO_InitStructure);
  664.     /* DAC channel1 Configuration */
  665.       DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
  666.       DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;
  667.       DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_2047;
  668.       DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  669.       DAC_Init(DAC_Channel_1, &DAC_InitStructure);

  670.       /* DAC channel2 Configuration */
  671.      
  672.       
  673.       DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;                       //不使用觸發功能TEN1=0
  674.       DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;         //不使用波形發生
  675.       DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
  676.       //DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;          //DAC1輸出緩存關閉
  677.        DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  678.       DAC_Init(DAC_Channel_2, &DAC_InitStructure);                                //初始化DAC通道1

  679.       /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is
  680.          automatically connected to the DAC converter. */
  681.       DAC_Cmd(DAC_Channel_1, ENABLE);

  682.       /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is
  683.          automatically connected to the DAC converter. */
  684.       DAC_Cmd(DAC_Channel_2, ENABLE);

  685.       /* Set DAC dual channel DHR12RD register */
  686.       DAC_SetDualChannelData(DAC_Align_12b_R, 0x100, 0x100);   
  687. }
  688. /*
  689. void ADC_init(void)
  690. {

  691.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);   
  692.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

  693.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  694.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;  
  695.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;  
  696.         GPIO_Init(GPIOA, &GPIO_InitStructure);

  697.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);
  698.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);
  699.        
  700.         ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  701.         ADC_CommonInitStructure.ADC_TwoSamplingDelay =  ADC_TwoSamplingDelay_5Cycles;   
  702.         ADC_CommonInitStructure.ADC_DMAAccessMode =  ADC_DMAAccessMode_Disabled;  
  703.         ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;  //84/4=21MHz   
  704.         ADC_CommonInit(&ADC_CommonInitStructure);
  705. ……………………

  706. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有資料51hei提供下載:
簡易電子秤.zip (569.63 KB, 下載次數: 23)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:343140 發表于 2019-1-21 20:38 | 只看該作者
樓主能QQ分享一下資料嗎,黑幣不夠無法下載
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 午夜电影一区二区 | 欧美区日韩区 | 手机看片在线播放 | 国产精品污www一区二区三区 | 国产精品一二三区 | 日韩一三区| 在线一区二区三区 | 亚洲国产精品一区二区第一页 | 国产精品久久久久久久久久久久久 | 91精品国产99| 日本不卡视频在线播放 | 中文字幕人成乱码在线观看 | 久久久91| 久久成人免费视频 | 综合二区 | www.com久久久 | 欧美一级网站 | 日韩精品在线观看免费 | 一级毛片观看 | 激情福利视频 | 一区二区三区久久 | 好姑娘高清在线观看电影 | 欧美性久久| 日本一区二区三区免费观看 | 91免费看片 | 欧美成人高清视频 | 亚洲精品无 | 亚洲成人一级片 | 日韩在线免费观看视频 | 成人免费视屏 | 成人在线观看免费 | 亚洲欧美视频一区 | 大伊人久久 | 日韩一级 | 九一视频在线播放 | 欧美激情网站 | 操操日| 精品一区二区三区四区五区 | 美女高潮网站 | 国产精品久久久久不卡 | 91五月婷蜜桃综合 |