標題: 用STM8做個頻率計 數(shù)碼管動態(tài)顯示 [打印本頁]

作者: 大魔王    時間: 2017-1-6 09:45
標題: 用STM8做個頻率計 數(shù)碼管動態(tài)顯示
/* MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/
#include "stm8s207R8.h"
#include "prohead.h"
unsigned long frequency;
unsigned int captureValue1,captureValue2;
unsigned char gewei,shiwei,baiwei,qianwei;
unsigned char dis[]={0xc0,0xf9,0xa4,0xb0,0x99,0x82,0xf8,0x80,0x90};
void TIM1_Init(void);
void CCO_Init(void);
void GPIO_Init(void);
void Display(void);
void Delay(unsigned int t);
void main()
{
unsigned int i;
CLK_CKDIVR = 0x00;
GPIO_Init();
CCO_Init();
TIM1_Init();
while(1)
{
  TIM1_CCER1 |= 0x01;
  while((TIM1_SR1 & 0x02) == 0);
  captureValue1 = (unsigned int)TIM1_CCR1H << 8;
  captureValue1 |= TIM1_CCR1L;
  while((TIM1_SR1 &0x02) ==0);
  captureValue2 = (unsigned int)TIM1_CCR1H << 8;
  captureValue2 |= TIM1_CCR1L;
  TIM1_CCER1 &= 0xfe;
  frequency = (8 * 16000000UL)/(captureValue2 - captureValue1);
  frequency =  frequency/100;
  for(i = 0;i<500;i++) Display();
}
}
void CCO_Init(void)
{
  CLK_CCOR |= 0x02;
  CLK_CCOR |= 0x01;
}

void TIM1_Init(void)
{
  TIM1_CCMR1 |= 0x01;
  TIM1_CCMR1 |= (0x03<<2);
   TIM1_CR1 |= 0x01;
}

void GPIO_Init(void)
{
  PG_ODR = 0xff;
  PG_DDR = 0xff;
  PG_CR1 = 0xff;
  PG_CR2 = 0xff;
  
  PB_ODR = 0xff;
  PB_DDR = 0xff;
  PB_CR1 = 0xff;
  PB_CR2 = 0xff;
  }
void Display(void)
{
qianwei = frequency/1000;
baiwei = (frequency%1000)/100;
shiwei = (frequency%100)/10;
gewei = frequency%10;
PG_ODR = dis[gewei];
PB_ODR = 0x080;
Delay(100);
PB_ODR = 0x00;
PG_ODR = dis[shiwei]&0x7f;
PB_ODR = 0x40;
Delay(100);
PG_ODR = dis[baiwei];
PB_ODR = 0x20;
Delay(100);
PG_ODR = dis[qianwei];
PB_ODR = 0x10;
Delay(100);
PB_ODR = 0x00;
}
void Delay(unsigned int t)
{
while(t--);
}
程序比較簡單 就不一一注釋啦


作者: robter    時間: 2018-2-24 10:37
這么簡單?不懂
作者: robter    時間: 2018-3-24 07:41
這個真的很簡單,要好好學學研究




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1