PIC stopwatch lab

Source: Internet
Author: User
Tags 0xc0

This experiment uses a digital tube to implement the stopwatch timing function, and uses buttons to pause, continue, reset,
Enable flashing of LED lights when the digital tube is suspended
1. the LED light is controlled by the C port and valid at a high level.
2. The bit segment of the digital tube is controlled by port A, and the data segment is controlled by port D.
3. Press the key to connect to port B.
Rb0 primary key
Rb2 continue key
Rb4 stopwatch timing start
4. Service Interruption
When the rb0 rst key is pressed, the stopwatch stops and starts to flash,
When Rb2 is pressed, it will be delayed for a moment, return to the stopwatch timing, and press the reset key to start from
Requirements: mplab8.5 or above compilation, mcd-2 downloader, PIC engineering board, turn off the watchdog timer, low voltage Programming
*/
# Include <p18cxxx. h>
******************** ************
Const unsigned int table [10] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99,
0x92, 0x82, 0xf8, 0x80, 0x90}; // 0-9 defines no decimal point

Const unsigned int Table1 [10] = {0x40, 0x79, 0x24, 0x30, 0x19,
0x12, 0x02, 0x78, 0x00, 0x10}; // {0-9} with a decimal point
********************* ***************
********************* *********
Void pic18f_high_isr (void); // interrupt the Service Program
Void pic18f_low_isr (void );
//************************************** *********************
Void delay (unsigned int X)
{
Unsigned int I;
For (I = x; I> 0; I --){;}
}

********************* *******
Void portcb_inio ()
{
Trisc = 0x00; // port C is all output
Portc = 0x00; // all the LEDs are disabled.
}

// *******************, D and B port initialization-digital tube **********************
Void port_inio ()
{
Adcon1 = 0xff; // Configure port A as a digital input
Trisa = 0xc0; // all ports a are output, A6, and A7 are input crystal oscillator frequencies.
Trisd = 0x00; // Port D is initialized to the output
Trisbbits. trisb0 = 1; // rb0, Rb1, and rb4 are set to suspend input.
Trisbbits. trisb2 = 1; // continue
Trisbbits. trisb4 = 1; // start timing key
Intcon2bits. rbpu = 0; // enables internal pull-up of port B.
Portc = 0x00; // all the LEDs are disabled.
 
Porta = 0x00; // initialize 6-digit Digital Display
Portd = 0xc0; // All initialized digital tubes are 0
}
******************* ******
Void led ()
{
Int K;
Int I;
Portcb_inio (); // port C initialization Function
Trisbbits. trisb2 = 1;
For (k = 1000; k> 0; k --)
{
// Sequential flash
Portc = 0x01; // light R0
Delay (1000); // latency
For (I = 8; I> 0; I --)
{
Portc = portc <1; // shifts one digit to the right.
Delay (1000); // latency
}
// Flashing
Portc = 0x00; // destroy
Portc = 0xff; // all bright
Delay (1000 );
Portc = 0x00; // destroy
Delay (1000 );

// Anti-flash

Portc = 0x80; // 10000000 light r7
Delay (1000); // latency

For (I = 0; I <8; I ++)
{
Portc = portc> 1;
Delay (1000); // latency
}
If (k = 0)
{
Break;
}
If (portbbits. Rb2 = 0) // press the button to exit the interrupt function and continue timing.
{
Delay (400); // eliminates Jitter
If (portbbits. Rb2 = 0)
{
Break;
}
}
}
}

// ******************** Declare the interrupt function *************** ********
//************************************** ********
# Pragma code high_vector_section = 0x8 // # pragma Code specifies the starting address of the program following the ROM as 0x8 and ends at 0x17.
Void high_vector ()
{
_ ASM goto pic18f_high_isr _ endasm // Use this command to escape to a high-priority interrupt service program
}
//************************************** ************
# Pragma code low_vector_section = 0x18
Void low_vector ()
{
_ ASM goto pic18f_low_isr _ endasm // Use this command to escape to a low-priority interrupt service program
}
//************************************** **************************
# Pragma code // return the default code segment.

********* ************
# Pragma interrupt pic18f_high_isr // # pragma interrupt keyword jump to the high interrupt service program. The following must be the next program
Void pic18f_high_isr () // remember to clear the interrupt flag
{

// Determine the same level of priority-flag bit
If (intconbits. int0if = 1) // rb0 pressed
{
Intconbits. int0if = 0; // iint0 External Interrupt flag intcon1 register written as intco
LED (); // flashing LED light
}

}

# Pragma interruptlow pic18f_high_isr // jump to the low-interrupt service program and remember to clearly identify the interrupt flag
Void pic18f_low_isr ()
{
// This instance has not been interrupted with a low priority

}

********************** *************
Void main ()
{
Int mseconds = 0; // initialization in milliseconds is 0
Int seconds = 0; // The seconds are initialized to 0.
Int minutes = 0; // initialize to 0 in minutes
Int COUNT = 0; // delay count variable
Port_inio (); // initialize ports A, D, and B
//************************************** ********************

// Interrupt function settings-rb0 and rb1.

// Set 1rb0 for high priority
Intcon2bits. intedg0 = 0; // enable intcon2 to enable external interruption 0 (rb0) to trigger edge interruption (source descent)
Intconbits. int0if = 0; // clear the priority of the External Interrupt mark
Intconbits. int0ie = 1; // enable int0 (used by rb0) External Interrupt 0 enable
// Intcon3bits. int0ip = 0; // the priority of the external interrupt is high. The int0ip does not exist. The default value is

Intconbits. gie = 1; // enable global interrupt

************************* **************
If (portbbits. rb4 = 0) // If rb4 is pressed
{
Delay (500); // delay to eliminate Jitter
If (portbbits. rb4 = 0) // If rb4 is indeed pressed, execute the following program
{
While (1)
{
+ Count; // The variable for the delay count starts to be added.
Porta = 0x3e; // open the first digital tube (ra0)
Portd = 0xff; // clear the function of refreshing the display
Portd = table [mseconds % 10]; // a bit in milliseconds
Delay (200); // short delay

Porta = 0x3d; // open the first 2nd digital tubes (ra1)
Portd = 0xff; // clear the function of refreshing the display
Portd = table [mseconds/10]; // 10 in milliseconds
Delay (200); // short delay

Porta = 0x3b; // after the first digital tube (ra2) is enabled, it has a decimal point in area, minute, second
Portd = 0xff; // clear the function of refreshing the display
Portd = Table1 [seconds % 10]; // a single digit in seconds
Delay (200); // short delay

Porta = 0x37; // open 4th digital tubes (ra3)
Portd = 0xff; // clear the function of refreshing the display
Portd = table [seconds/10]; // 10 of the second
Delay (200); // short delay

Porta = 0x2f; // after the first digital tube (ra4) is enabled, it has a decimal point in the area, minute, second
Portd = 0xff; // clear the function of refreshing the display
Portd = Table1 [minutes % 10]; // a single point
Delay (200); // short delay

Porta = 0x1f; // open the 6th digital tubes (ra5)
Portd = 0xff; // clear the function of refreshing the display
Portd = table [minutes/10]; // ten points
Delay (200); // short delay

If (COUNT = 1) // 1us
{
Count = 0;
+ + Mseconds; // starts counting in seconds.
Seconds + = mseconds/60; // when S is 60, M = 60/60 = 1
Mseconds = mseconds % 60; // 1% 60 = 1
Minutes + = minutes/60;
Seconds = seconds % 60;
Minutes = minutes % 60;
}
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.