#pragma config PLLDIV = 2, CPUDIV = OSC1_PLL2, USBDIV = 2
#pragma config FOSC = HSPLL_HS, FCMEN = OFF, IESO = OFF
#pragma config PWRT = OFF, BOR = OFF, BORV = 3, VREGEN = OFF
#pragma config WDT = OFF
#pragma config WDTPS = 32768
#pragma config CCP2MX = ON, PBADEN = OFF, LPT1OSC = OFF, MCLRE = ON
#pragma config STVREN = ON, LVP = OFF, ICPRT = OFF, XINST = OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CPB = OFF, CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTC = OFF, WRTB = OFF, WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF
#define _XTAL_FREQ 48000000
#include <xc.h>
#include <stdint.h>
#define CLK_DIR TRISBbits.RB0
#define DATA1_DIR TRISBbits.RB1
#define DATA2_DIR TRISBbits.RB2
#define LATCH_DIR TRISBbits.RB3
#define CLK_PIN LATBbits.LB0
#define DATA1_PIN LATBbits.LB1
#define DATA2_PIN LATBbits.LB2
#define LATCH_PIN LATBbits.LB3
uint8_t estado = 0;
uint8_t cont_main = 0;
uint8_t cont_timer_1 = 0;
uint8_t cont_peaton = 0;
uint8_t cont_blink = 0;
uint8_t flag_run = 0;
uint16_t speed_run = 35536;
const uint8_t number_count[20][8] = {
0x88, 0xea, 0x8a, 0xba, 0x88, 0xff, 0xff, 0xff, // 20
0x98, 0xda, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff, // 19
0x98, 0xda, 0xd8, 0xda, 0x88, 0xff, 0xff, 0xff, // 18
0x98, 0xde, 0xde, 0xde, 0x8e, 0xff, 0xff, 0xff, // 17
0x98, 0xdb, 0xd8, 0xda, 0x88, 0xff, 0xff, 0xff, // 16
0x98, 0xdb, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff, // 15
0x9a, 0xda, 0xd8, 0xde, 0x8e, 0xff, 0xff, 0xff, // 14
0x98, 0xde, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff, // 13
0x98, 0xde, 0xd8, 0xdb, 0x88, 0xff, 0xff, 0xff, // 12
0x99, 0xdd, 0xdd, 0xdd, 0x88, 0xff, 0xff, 0xff, // 11
0x98, 0xda, 0xda, 0xda, 0x88, 0xff, 0xff, 0xff, // 10
0x88, 0xaa, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff, // 9
0x88, 0xaa, 0xa8, 0xaa, 0x88, 0xff, 0xff, 0xff, // 8
0x88, 0xae, 0xae, 0xae, 0x8e, 0xff, 0xff, 0xff, // 7
0x88, 0xab, 0xa8, 0xaa, 0x88, 0xff, 0xff, 0xff, // 6
0x88, 0xab, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff, // 5
0x8a, 0xaa, 0xa8, 0xae, 0x8e, 0xff, 0xff, 0xff, // 4
0x88, 0xae, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff, // 3
0x88, 0xae, 0xa8, 0xab, 0x88, 0xff, 0xff, 0xff, // 2
0x89, 0xad, 0xad, 0xad, 0x88, 0xff, 0xff, 0xff // 1
};
const uint8_t peaton_run[4][8] = {
0xcf, 0xcf, 0xe7, 0xe7, 0xe7, 0xe7, 0xf7, 0xf7,
0xcf, 0xcf, 0xe7, 0xe3, 0xc3, 0xc7, 0xdb, 0xfb,
0xcf, 0xcf, 0xe3, 0x85, 0xe7, 0xdb, 0xdd, 0xdf,
0xcf, 0xcf, 0xe7, 0xe7, 0xc7, 0xe7, 0xe3, 0xef
};
const uint8_t peaton_stop[8] = {0xe3, 0xe3, 0xf7, 0xe3, 0xd5, 0xf7, 0xeb, 0xeb};
const uint8_t desp[8] = {0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F};
void Send_Byte_Data(uint8_t b_m1, uint8_t b_m2);
void main()
{
ADCON1bits.PCFG = 0x0F; // Configura todos los pines como digitales
TRISAbits.RA0 = 0; // Pin RA0 como salida (led verde)
TRISAbits.RA1 = 0; // Pin RA1 como salida (led amarillo)
TRISAbits.RA2 = 0; // Pin RA2 como salida (led rojo)
CLK_DIR = 0; // Pin CLK
DATA1_DIR = 0; // Pin DATA (CI1)
DATA2_DIR = 0; // Pin DATA (CI2)
LATCH_DIR = 0; // Pin LATCH
LATAbits.LA0 = 0;
LATAbits.LA1 = 0;
LATAbits.LA2 = 0;
CLK_PIN = 0;
DATA1_PIN = 0;
DATA2_PIN = 0;
LATCH_PIN = 0;
TRISD = 0x00; // Puerto D como salida
LATD = 0x00; // Limpia le puerto D
RCONbits.IPEN = 1; // Habilita los niveles de interrupcion
INTCONbits.GIEH = 1; // Habilita la prioridad alta en interrupciones
INTCONbits.GIEL = 1; // Habilita la prioridad baja en interrupciones
T0CON = 0x07; // Configuracion del timer 0
T1CON = 0xF8; // Configuracion del timer 1
INTCONbits.TMR0IE = 1; // Habilita la interrupcion del timer 0
INTCON2bits.TMR0IP = 1; // Alta prioridad para la interrupcion del timer 0
INTCONbits.TMR0IF = 0; // Flag 0 (timer 0)
PIE1bits.TMR1IE = 1; // Habilita la interrupcion del timer 1
IPR1bits.TMR1IP = 0; // Baja prioridad para la interrupcion del timer 1
PIR1bits.TMR1IF = 0; // Flag 0 (timer 1)
TMR0 = 18661;
TMR1 = 35536;
T0CONbits.TMR0ON = 1; // Habilita el timer 0
T1CONbits.TMR1ON = 1; // Habilita el timer 1
while(1)
{
switch(estado)
{
case 0:
LATAbits.LA0 = 0;
LATAbits.LA1 = 0;
LATAbits.LA2 = 1;
for(uint8_t i=0; i<8; i++){
Send_Byte_Data(peaton_stop[i], 0xFF);
LATD = desp[i];
__delay_us(800);
}
break;
case 1:
for(uint8_t i=0; i<8; i++){
Send_Byte_Data(peaton_stop[i], 0xFF);
LATD = desp[i];
__delay_us(800);
}
cont_blink++;
if(cont_blink == 80){
LATAbits.LA2 = !LATAbits.LA2;
cont_blink = 0;
}
break;
case 2:
cont_blink = 0;
LATAbits.LA0 = 0;
LATAbits.LA1 = 1;
LATAbits.LA2 = 0;
for(uint8_t i=0; i<8; i++){
Send_Byte_Data(peaton_stop[i], 0xFF);
LATD = desp[i];
__delay_us(800);
}
break;
case 3:
LATAbits.LA0 = 1;
LATAbits.LA1 = 0;
LATAbits.LA2 = 0;
for(uint8_t i=0; i<8; i++){
Send_Byte_Data(number_count[cont_main][i], peaton_run[cont_peaton][i]);
LATD = desp[i];
__delay_us(800);
}
break;
}
}
}
void __interrupt(high_priority) INT_TMR0()
{
if(INTCONbits.TMR0IF == 1)
{
cont_main++;
if(cont_main == 5 && estado == 0){
cont_main = 0;
estado = 1;
}
else if(cont_main == 4 && estado == 1){
cont_main = 0;
estado = 2;
}
else if(cont_main == 1 && estado == 2){
cont_main = 0;
estado = 3;
flag_run = 1;
}
else if(cont_main == 17 && flag_run == 1){
speed_run = 50536;
flag_run = 0;
}
else if(cont_main == 20 && estado == 3){
cont_main = 0;
estado = 0;
speed_run = 35536;
}
TMR0 = 18661;
INTCONbits.TMR0IF = 0;
}
}
void __interrupt(low_priority) INT_TMR1()
{
if(PIR1bits.TMR1IF == 1)
{
cont_timer_1++;
if(cont_timer_1 == 9){
cont_timer_1 = 0;
cont_peaton++;
if(cont_peaton > 3){
cont_peaton = 0;
}
}
TMR1 = speed_run;
PIR1bits.TMR1IF = 0;
}
}
void Send_Byte_Data(uint8_t b_m1, uint8_t b_m2)
{
for(uint8_t i=0x80; i>0; i=i>>1)
{
(b_m1 & i) ? (DATA1_PIN = 1) : (DATA1_PIN = 0);
(b_m2 & i) ? (DATA2_PIN = 1) : (DATA2_PIN = 0);
CLK_PIN = 1;
CLK_PIN = 0;
}
LATCH_PIN = 1;
LATCH_PIN = 0;
}