Hello everyone
I am trying to write program using the PIC18F45K80 microcontroller that controls a set of LEDs connected to PORTB pins. The LEDs should exhibit three states: OFF, ON, and BLINKING. The program should , turning off the LEDs in the OFF state, turning them on in the ON state, and making them blink five times in the BLINKING state.
I have written simple program on tested on PC
PC program
Output
ALL Lights ON
This program should turning ON all LEDs using a switch-case structure and an enum but I don't understand why LEDs are not turning on the state "ON" ?. I am sure there is no problem in connection.
Embedded C Program
configuartion file
I am trying to write program using the PIC18F45K80 microcontroller that controls a set of LEDs connected to PORTB pins. The LEDs should exhibit three states: OFF, ON, and BLINKING. The program should , turning off the LEDs in the OFF state, turning them on in the ON state, and making them blink five times in the BLINKING state.
I have written simple program on tested on PC
PC program
C:
#include<stdio.h>
enum LightState {OFF, ON, BLINKING};
enum LightState state;
int main()
{
switch ( state = ON)
{
case OFF:
printf("ALL Lights OFF \n");
break;
case ON:
printf("ALL Lights ON \n");
break;
case BLINKING:
printf("ALL Lights Blinking \n");
break;
}
return 0;
}
ALL Lights ON
This program should turning ON all LEDs using a switch-case structure and an enum but I don't understand why LEDs are not turning on the state "ON" ?. I am sure there is no problem in connection.
Embedded C Program
C:
// MPLAB XC8 PIC19F45K80
#include <xc.h>
#include <stdio.h>
#include "config.h"
// Define LED states using an enum
enum LEDState {
OFF,
ON,
BLINKING
};
void main(void) {
TRISB = 0x00; // Configure all pins of PORTB as outputs
LATB = 0x00; // Initialize PORTB to low
while (1) {
enum LEDState state = ON; // Initial LED state
switch (state) {
case OFF:
LATB = 0x00; // Turn off LEDs
break;
case ON:
LATB = 0xFF; // Turn on LEDs
break;
case BLINKING:
for (int i = 0; i < 5; i++) {
LATB = 0xFF; // Turn on LEDs
__delay_ms(500); // Delay for 500 ms
LATB = 0x00; // Turn off LEDs
__delay_ms(500); // Delay for 500 ms
}
break;
}
}
return;
}
Code:
config.h
#define _XTAL_FREQ 20000000
config.c
// PIC18F45K80 Configuration Bit Settings
// CONFIG1L
#pragma config RETEN = ON // VREG Sleep Enable bit (Ultra low-power regulator is Enabled (Controlled by SRETEN bit))
#pragma config INTOSCSEL = LOW // LF-INTOSC Low-power Enable bit (LF-INTOSC in Low-power mode during Sleep)
// SOSCSEL = No Setting
#pragma config XINST = OFF // Extended Instruction Set (Disabled)
// CONFIG1H
#pragma config FOSC = HS2 // HS oscillator (high power, 16 MHz-25 MHz
#pragma config PLLCFG = OFF // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = ON // Power Up Timer (Enabled)
#pragma config BOREN = OFF // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0 // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = LOW // BORMV Power level (BORMV set to low power level)
// CONFIG2H
#pragma config WDTEN = OFF // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1 // Watchdog Postscaler (1:1)
// CONFIG3H
#pragma config CANMX = PORTC // ECAN Mux bit (ECAN TX and RX pins are located on RC6 and RC7, respectively)
#pragma config MSSPMSK = MSK5 // MSSP address masking (5 bit address masking mode)
#pragma config MCLRE = ON // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = OFF // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K // Boot Block Size (1K word Boot Block size)
// CONFIG5L
#pragma config CP0 = ON // Code Protect 00800-01FFF (Enabled)
#pragma config CP1 = ON // Code Protect 02000-03FFF (Enabled)
#pragma config CP2 = ON // Code Protect 04000-05FFF (Enabled)
#pragma config CP3 = ON // Code Protect 06000-07FFF (Enabled)
// CONFIG5H
#pragma config CPB = ON // Code Protect Boot (Enabled)
#pragma config CPD = ON // Data EE Read Protect (Enabled)
// CONFIG6L
#pragma config WRT0 = ON // Table Write Protect 00800-01FFF (Enabled)
#pragma config WRT1 = ON // Table Write Protect 02000-03FFF (Enabled)
#pragma config WRT2 = ON // Table Write Protect 04000-05FFF (Enabled)
#pragma config WRT3 = ON // Table Write Protect 06000-07FFF (Enabled)
// CONFIG6H
#pragma config WRTC = ON // Config. Write Protect (Enabled)
#pragma config WRTB = ON // Table Write Protect Boot (Enabled)
#pragma config WRTD = ON // Data EE Write Protect (Enabled)
// CONFIG7L
#pragma config EBTR0 = ON // Table Read Protect 00800-01FFF (Enabled)
#pragma config EBTR1 = ON // Table Read Protect 02000-03FFF (Enabled)
#pragma config EBTR2 = ON // Table Read Protect 04000-05FFF (Enabled)
#pragma config EBTR3 = ON // Table Read Protect 06000-07FFF (Enabled)
// CONFIG7H
#pragma config EBTRB = ON // Table Read Protect Boot (Enabled)
// #pragma config statements should precede project file includes.
