PIC16F877A development board digital tube counter experiment

Publisher:Changfeng520Latest update time:2016-12-07 Source: eefocusKeywords:PIC16F877A Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//*******************Electronic Park PIC16F877A development board digital tube counter experiment*****************
//
//CKP=0; Idle is low level
//CKP=1; Idle is high level
//STAT_CKE=0; SDO trailing edge sampling
//STAT_CKE=1; SDO leading edge sampling
//
//STAT_SMP=0; //Sampling input data in the middle of data output time
//STAT_SMP=1; //Sampling input data at the end of data output time
//
//RC3/SCK connects to 74595 shift clock pin 11
//RC5/SDO connects to 74595 data input pin 14
//RC4/SDI connects to 74595 latch clock pin 12
//
//When TMR0 is used as a timer, timer clock = system clock/4
//When writing TMR0, a 2-cycle delay will be generated. If the pre-divider is not used, it can be compensated by setting the initial value
//For example, when 200 clock cycles are required, TMR0=256-200+2=58
//
//Use TMR0 to realize dynamic scanning of digital tubes, lighting up one digital tube every 1ms
//
//mcu: PIC16F877A 4MHz Electronic Park PIC16F877A development board
//December 23, 2010 19:22:30
//***************************************************************************
 
#include
__CONFIG(HS&WDTDIS&LVPDIS&PWRTEN);   
//HS oscillation, disable watchdog, low voltage programming shutdown, start delay timer                        
                                            
fly disbuf[8];
uint count=0;
uchar time_ok=0; 
 
//********************Function definition*********************
void delay1ms(uint DelayTime);    
uchar spi_write_read(uchar dd);
void led_display(void);
void decimal_bcd_16bit(uint cnt);
 
 
//*************** Main program************************
void main(void)
{   
  SSPM3=0;
  SSPM2=0;
  SSPM1=0;
  SSPM0=0; //spi master mode, clock is Fosc/4
   
  CKP=0; //Idle is low level
  STAT_CKE=1; //SDO leading edge sampling
   
  STAT_SMP=0; // Sample input data in the middle of data output time
   
  TRISC3=0; //RC3/SCK is output
  TRISC5=0; //RC5/SDO is output
  TRISC4=0; //RC4/SDI is output (when the SDI pin is not used, it can be set to output for normal IO use)
  RC4=0;
  SSPEN=1; //Enable MSSP module, enable spi or iic
   
  PSA=1; //The pre-divider is assigned to WDT (TMR0 clock is not divided)
  T0CS=0; //Set to timing mode
  TMR0=256-200+2; //timing 200 clocks            
   
  GIE=1; //General interrupt enabled
  T0IE=1; //TMR0 interrupt enable
   
  delay1ms(10);
 
 
  while(1)
  {
    if(time_ok)
    {
      time_ok=0;
      count=count+1;    
      decimal_bcd_16bit(count);            
    }              
  }
}   
 
//****************spi bus sends and receives data****************
uchar spi_write_read(uchar dd)
{
  flying buff;
  SSPBUF=dd;
  while(!STAT_BF); //Wait for data to be sent and received
  buf=SSPBUF;         
  return (buf); //Return the received data
}
 
void led_display(void)
{
  const float smg[]={0x0a,0xfa,0x8c,0xa8,0x78,0x29,0x09,0xba,0x08,0x28,0x00,};                       
                    // 0    1    2    3    4    5    6    7    8    9   all
  const uchar smg_bit[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0xff,};
  static uchar st=0;
   
  RC4=0;
  spi_write_read(smg_bit[st]);    
  spi_write_read(smg[disbuf[st]]);
  RC4=1; 
   
  st=(++st)%8;
 
void decimal_bcd_16bit(uint cnt)
{
  disbuf[4]=(cnt/1000)%10;
  cnt=cnt%1000;
  disbuf[5]=(cnt/100)%10;       
  cnt=cnt%100;
  disbuf[6]=cnt/10; //divide by 10
  disbuf[7]=cnt%10; //Get the unit digit
   
  disbuf[0]=disbuf[4];
  disbuf[1]=disbuf[5];
  disbuf[2]=disbuf[6];
  disbuf[3]=disbuf[7];
 
 
//*********************Interrupt service routine******************
void interrupt isr(void)
{
  static uchar time_con1=0;
  static uint time_con2=0;
   
  if(T0IE&&T0IF) //Judge whether it is TMR0 interrupt
  {
    T0IF=0; // Clear TMR0 interrupt flag (must be cleared by software)   
    TMR0=TMR0+58; //Re-initialize TMR0
    if(++time_con1>=5) //Timer 1ms each time
    {
      time_con1=0;
      led_display();
    }
     
    if(++time_con2>=500) //Counter increments by 1 every 0.1s
    {
      time_con2=0;
      time_ok=1;   
    }
  }   
}
 
 
//********************Delay n*1ms 12MHz*****************
void delay1ms(uint DelayTime)
{   
  uint temp;
  for(;DelayTime>0;DelayTime--)
  {   
    for(temp=0;temp<270;temp++)
    {;}
  }
}


Keywords:PIC16F877A Reference address:PIC16F877A development board digital tube counter experiment

Previous article:PIC16F877A development board common IO driver 74595 experiment
Next article:PIC16F877A development board digital tube dynamic scanning experiment

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号