//************************************** ******************************
/* Press the sb3 key (Rb3 port) on the picdemo2 lab board at a time to increase the value by 1. */
/* Source file name 18f552_portb_c18.c
/* Happy
Led cycle display 01-99, using pic18f4520 device. */
/* Use the 18 compiler, and mplab v8.2 passes debugging. */
/*************************************** *********************************
Name: 1-99
Copyright: Tang xiangheng
Author: Tang xiangheng
Date: 22-10-09 :46
Description:
Function: use port C to display the 1-99 Flow lights.
Device: pic18f4520 chip, PIC-demo engineering board, picmcd2 simulator, desktop computer
Mplab9.6 passed the test
Requirements: XT crystal oscillator is used to disable the watchdog timer and low-voltage programming.
**************************************** **********************************/
# Include <pic18.h>/* flag header file */
//************************************** ******************************
# Define uchar unsigned char
# Define uint unsigned int
//************************************** ******************************
Void initportc ();
Void initportb ();
Void uinttouchar (uchar X); // function declaration
Uchar connectbyte (uchar Y1, uchar Y2); // combines high and low BCD bytes.
Void delay (); // declares the key delay function.
//************************************** ******************************
Uchar counter; // key count.
Uchar x1, x2; // X1 is BCD 4-bit high, and X2 is BCD 4-bit low.
//************************************** ******************************
Void main (void)
{
Initportb ();
Initportc ();
Counter = 0; // set the Initial Value
While (1) // scan the keyboard repeatedly
{
While (1) // scan the keyboard repeatedly
{
If (Rb3 = 0) // whether a key is pressed
{
Delay ();/* delay of 10 ms */
If (Rb3 = 0)
{
Counter ++; // the key value is increased by 1.
Uinttouchar (Counter); // convert the hexadecimal number to the decimal number.
Portc = connectbyte (x1, x2); // merge the BCD code with four high and low bits
Break; // exit.
}
}
}
While (1)
{
If (Rb3 = 1) // press the button to release it?
Delay (); // delay: 10 ms.
If (Rb3 = 1) // press the button to release it.
Break; // exit.
}
}
}
//************************************** ******************************
// Initialize portc.
//************************************** ******************************
Void initportc ()
{
Trisc = 0x00; // set the C port to output
Portc = 0x00; // close the C port.
}
//************************************** ******************************
// Initialize portb.
//************************************** ******************************
Void initportb ()
{
Portb = 0x00; // clear port.
Intcon = 0x00; // close all interrupts
Adcon1 = 0x07; // set port B to switch I/O.
Trisb = 0xff; // set port B as the input.
// Porta = 0xff;
}
//************************************** ******************************
// The delay is 10 ms.
//************************************** ******************************
Void delay ()
{
Uint d = 1000;
While (-- d)
{;}
}
//************************************** ******************************
// Convert hexadecimal to hexadecimal.
//************************************** ******************************
Void uinttouchar (uchar X)
{
Uchar mid, Y;
Mid = X;
Y = X;
X2 = (uchar) (Y % 10 );
Y = mid;
X1 = (uchar) (y-x2) % 100)/10 );
}
//************************************** ******************************
// Merge bytes.
//************************************** ******************************
Uchar connectbyte (uchar Y1, uchar Y2)
{
Return (0xf0) & (Y1 <4) | (0x0f) & Y2 ));
}
//************************************** ******************************