PIC stuck in allways HIGH Out.

Thread Starter

FroceMaster

Joined Jan 28, 2012
632
Hi
Been struggeling a while.
This code for 16F18877 is making my RB2 allways high, and cant figure it out.
The output is steady, measured with scope, and can easy drive a LED.

Code:
// PIC16F18877 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = OFF    // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = EXT1X   // Power-up default value for COSC bits (EXTOSC operating per FEXTOSC bits)
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON       // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON      // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)

// CONFIG2
#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF        // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

// CONFIG4
#pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

// CONFIG5
#pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdbool.h>
#include <stdint.h>

uint8_t UART_Buffer = 0; // is a 8bit interger / 1.byte
uint8_t data,ID,trash,NightContrastTime,DayContrastTime;
int nytid,contrast,sec,minut,hour,x,i,on_contrast,off_contrast,no90_opens,runs_code90,ReadyToCode90,newsec,newminut,newhour;
int ReadyToCode80,no80_opens,runs_code80,run_first_time,night;

#define _XTAL_FREQ 32000000

void __interrupt() myISR(void)
{// things to do
    // only interrupt is from RX EUSART
    if (RCIF==1)
   {
   CREN=0;
   UART_Buffer=RCREG;
   data=1;
   CREN=1;
 
  
  }
}


void setup_system (void)
{
    //setup clock.
    OSCCON1=0x60; //OSC Settings
    OSCFRQ=0x06; // 32 mhz
    PMD0=0x00; // all modules enab.
    PMD1=0xff; // all disable, hence not using it.
    PMD2=0x67; // all disable, not using
    PMD3=0x7F; //all disable
    PMD4=0x37; // EUSART module enabled all other disable
    PMD5=0xDF; // all disable
    LATA=0x00;
    LATB=0x00;
    LATC=0x00;       
    LATD=0x00;
    LATE=0x00;
    TRISA=0xFF;      // all input only use 0+3
    TRISB=0xD8;      // out = B0-B1-B2-B5, IN = B3-B4-B6-B7
    TRISC=0x00;      // all output
    TRISD=0x00;      // all output
    TRISE=0x07;      // all input
    ANSELD=0x00; // all digital , no analog function
    ANSELC=0x00;
    ANSELB=0x00;
    ANSELE=0x00;
    ANSELA=0x00;
    ODCONB=0x00;
    ODCONC=0x00;
    ODCOND=0x00;
    RXPPS=0x0B; // MOVE RX to RB3.  RB3 = 0x0B
    SSP2DATPPS=0x0C; // try to move it for test, using default RB2.  // still no luck
  
    // setup RX system
      // ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled;
    BAUD1CON = 0x08;
    // SPEN enabled; RX9 8-bit; CREN disabled; ADDEN disabled; SREN disabled;
    RC1STA = 0x80;
    // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
    TX1STA = 0x24;

    SP1BRGL = 0xBF; //191 aka 10417 baud
    // SP1BRGH 0;
    SP1BRGH = 0x00;
// next 4 disable for testing right now..   
   // CREN=1;
   // RCIE=1;
 //   PEIE=1;
    //GIE=1;
}

void main(void)
{
    setup_system();
 data=0;
    while(1)
    {
__delay_ms(2);
}
}
Any hint of where the big bug is ?

rgds.
 

BobaMosfet

Joined Jul 1, 2009
1,208
Hi
Been struggeling a while.
This code for 16F18877 is making my RB2 allways high, and cant figure it out.
The output is steady, measured with scope, and can easy drive a LED.

Code:
// PIC16F18877 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = OFF    // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = EXT1X   // Power-up default value for COSC bits (EXTOSC operating per FEXTOSC bits)
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON       // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON      // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)

// CONFIG2
#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF        // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

// CONFIG4
#pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

// CONFIG5
#pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdbool.h>
#include <stdint.h>

uint8_t UART_Buffer = 0; // is a 8bit interger / 1.byte
uint8_t data,ID,trash,NightContrastTime,DayContrastTime;
int nytid,contrast,sec,minut,hour,x,i,on_contrast,off_contrast,no90_opens,runs_code90,ReadyToCode90,newsec,newminut,newhour;
int ReadyToCode80,no80_opens,runs_code80,run_first_time,night;

#define _XTAL_FREQ 32000000

void __interrupt() myISR(void)
{// things to do
    // only interrupt is from RX EUSART
    if (RCIF==1)
   {
   CREN=0;
   UART_Buffer=RCREG;
   data=1;
   CREN=1;

 
  }
}


void setup_system (void)
{
    //setup clock.
    OSCCON1=0x60; //OSC Settings
    OSCFRQ=0x06; // 32 mhz
    PMD0=0x00; // all modules enab.
    PMD1=0xff; // all disable, hence not using it.
    PMD2=0x67; // all disable, not using
    PMD3=0x7F; //all disable
    PMD4=0x37; // EUSART module enabled all other disable
    PMD5=0xDF; // all disable
    LATA=0x00;
    LATB=0x00;
    LATC=0x00;      
    LATD=0x00;
    LATE=0x00;
    TRISA=0xFF;      // all input only use 0+3
    TRISB=0xD8;      // out = B0-B1-B2-B5, IN = B3-B4-B6-B7
    TRISC=0x00;      // all output
    TRISD=0x00;      // all output
    TRISE=0x07;      // all input
    ANSELD=0x00; // all digital , no analog function
    ANSELC=0x00;
    ANSELB=0x00;
    ANSELE=0x00;
    ANSELA=0x00;
    ODCONB=0x00;
    ODCONC=0x00;
    ODCOND=0x00;
    RXPPS=0x0B; // MOVE RX to RB3.  RB3 = 0x0B
    SSP2DATPPS=0x0C; // try to move it for test, using default RB2.  // still no luck
 
    // setup RX system
      // ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled;
    BAUD1CON = 0x08;
    // SPEN enabled; RX9 8-bit; CREN disabled; ADDEN disabled; SREN disabled;
    RC1STA = 0x80;
    // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
    TX1STA = 0x24;

    SP1BRGL = 0xBF; //191 aka 10417 baud
    // SP1BRGH 0;
    SP1BRGH = 0x00;
// next 4 disable for testing right now..  
   // CREN=1;
   // RCIE=1;
//   PEIE=1;
    //GIE=1;
}

void main(void)
{
    setup_system();
data=0;
    while(1)
    {
__delay_ms(2);
}
}
Any hint of where the big bug is ?

rgds.
Stop staring at code and start writing a flow-chart. A flow-chart that exactly reflects what the code does, not what you think it does (easy to get into that trap), will let you see the logic error immediately. If you are unwilling to use flow-charts, you should consider a different hobby as programming and embedded development is not for you. That's just being truthful.
 

Alec_t

Joined Sep 17, 2013
11,641
I'm no programmer, but don't lines 120-123 leave the gubbins stuck in an endless delay loop, in the absence of an ISR which would alter RB2?
 

AlbertHall

Joined Jun 4, 2014
10,393
In the simulator, PORTB is always 0x00.
The interrupt routine does not reset RCIF but I doubt that will make any difference as the interrupt routine does nothing.
Is the serial input receiving anything?
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
632
I think it do, i did have an LED connected, and when recieving, the led shifted status ,

This interrupt works correct in another project,
 
One thing that has caused me problems before I thought I would point out. You don't have your TX PPS set. I know the datasheet gives a default location, but I've found on some others I have messed with they aren't an actual default.

Other than that either something connected has burned up the pin drivers and it's being constantly powered, or who knows exactly?!?
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
632
Think i maybee will find the bug.

Can i use this
#define sec0 PORTC=0b10000000;PORTD=0b00000000;RB2=1;RB0=0;RB1=0;RB5=1

and in main code refer to this like this ?

if ((sec>52) || (sec<8))
{

switch (sec) {
case 0:
sec0;
break;
case 2:
sec2;
break;

ect.....

Or do the "define" only use the first statement ? ex PORTC ??
 

AlbertHall

Joined Jun 4, 2014
10,393
#define sec0 PORTC=0b10000000;PORTD=0b00000000;RB2=1;RB0=0;RB1=0;RB5=1
I think that will replace all occurences 'sec0' with 'PORTC=0b10000000;PORTD=0b00000000;RB2=1;RB0=0;RB1=0;RB5=1' Which clearly isn't going to do anything useful.
You will need to use separate #defines for each one.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
632
So u "say" that i will have to like this , or similar.
Code:
if ((sec>52) || (sec<8))
{
RB2=1;
RB0=0;
RB1=0;
RB5=1;
switch (sec) {
case 0:
PORTC=0b10000000;
PORTD=0b00000000;
break;
case 2:
PORTC=0b01000000;
PORTD=0b00000000;

break;
Some long switc-case, but maybe that is ok.

Edit : move the RB0-1-2+5 up in code, same on each case
 

AlbertHall

Joined Jun 4, 2014
10,393
Some long switc-case, but maybe that is ok.
What really matters is what the assembler code is and if you only want to change those four bits of portb leaving the rest unchanged then the code in post #18 will do that.
An alternative would be:
PORTB &= 0b11111100; // Set RB0 and RB1 to '0'
PORTB |= 0b00100100; // Set RB2 and RB5 to '1'
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
632
Hi again,
Have been changing code, and still RB0 - 1 -2 gets stuck in HIGH,
Output is constant 5v. Scope.
When measured on Port C and D. the output is a fine square wave at 2.2 ms on and 4,4 ms off = 6.6 ms ~150.8 hz, says scope.
But still my 3 transistors connected to RB0-1-2 is stucked,

Here is my code,
Still need som fine tuning, ,ex using Case instead of all my IF reg. "contrast level".
Code:
// PIC16F18877 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = OFF    // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = EXT1X   // Power-up default value for COSC bits (EXTOSC operating per FEXTOSC bits)
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON       // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON      // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)

// CONFIG2
#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF        // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

// CONFIG4
#pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

// CONFIG5
#pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdbool.h>
#include <stdint.h>

uint8_t UART_Buffer = 0; // is a 8bit interger / 1.byte
uint8_t data,ID,trash,NightContrastTime,DayContrastTime;
int nytid,contrast,sec,minut,hour,x,i,on_contrast,off_contrast,no90_opens,runs_code90,ReadyToCode90,newsec,newminut,newhour;
int ReadyToCode80,no80_opens,runs_code80,run_first_time,night;
#define _XTAL_FREQ 32000000

void __interrupt() myISR(void)
{// things to do
    // only interrupt is from RX EUSART
    if (RCIF==1)
   {
   CREN=0;
   UART_Buffer=RCREG;
   data=1;
   CREN=1;
 
  
  }
}


void setup_system (void)
{
    //setup clock.
    OSCCON1=0x60; //OSC Settings
    OSCFRQ=0x06; // 32 mhz
    PMD0=0x00; // all modules enab.
    PMD1=0xff; // all disable, hence not using it.
    PMD2=0x67; // all disable, not using
    PMD3=0x7F; //all disable
    PMD4=0x37; // EUSART module enabled all other disable
    PMD5=0xDF; // all disable
    LATA=0x00;
    LATB=0x00;
    LATC=0x00;       
    LATD=0x00;
    LATE=0x00;
    TRISA=0xFF;      // all input only use 0+3
    TRISB=0xD8;      // out = B0-B1-B2-B5, IN = B3-B4-B6-B7
    TRISC=0x00;      // all output
    TRISD=0x00;      // all output
    TRISE=0x07;      // all input
    ANSELD=0x00; // all digital , no analog function
    ANSELC=0x00;
    ANSELB=0x00;
    ANSELE=0x00;
    ANSELA=0x00;
    ODCONB=0x00;
    ODCONC=0x00;
    ODCOND=0x00;
    RXPPS=0x0B; // MOVE RX to RB3.  RB3 = 0x0B
    SSP2DATPPS=0x0C; // try to move it for test, using default RB2.  // still no luck
  
    // setup RX system
      // ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled;
    BAUD1CON = 0x08;
    // SPEN enabled; RX9 8-bit; CREN disabled; ADDEN disabled; SREN disabled;
    RC1STA = 0x80;
    // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
    TX1STA = 0x24;

    SP1BRGL = 0xBF; //191 aka 10417 baud
    // SP1BRGH 0;
    SP1BRGH = 0x00;
// next 4 disable for testing right now..   
   // CREN=1;
   // RCIE=1;
 //   PEIE=1;
    //GIE=1;
}
void main(void)
{
    setup_system();
nytid=0;
ID=0; // removes
    sec=1;
    minut=2;
    hour=3;
    contrast=5;// end removes
    data=0;  // UART part not completeed yet
    while(1)
    {
if (nytid==1)// only if new time is updated, else do normal loop.
        {  // calculate new values.
            sec=newsec;
            minut=newminut;
            hour=newhour;
            
            //use contrast level according to day/night
            if((hour>=NightContrastTime) || (hour<=DayContrastTime)) // night time, kill the lights :)
               {
                   // night now
                night=1;   
                
               }
            else
            {
                night=0;//day again
            }
              
               nytid=0;
              
        }
      
        if (ID==0) 
          {
            
            if ((sec>52) || (sec<8))
            {
               RB2=1;
               RB0=0;
               RB1=0;
               RB5=1;
                switch (sec) {
                    case 53:
                        PORTC=0b00000001;
                        PORTD=0b00000000;
                        break;
                    case 54:
                        PORTC=0b00000010;
                        PORTD=0b00000000;
                        break;
                    case 55:
                        PORTC=0b00000100;
                        PORTD=0b00000000;
                        break;
                    case 56:
                        PORTC=0b00001000;
                        PORTD=0b00000000;
                        break;
                    case 57:
                        PORTC=0b00010000;
                        PORTD=0b00000000;
                        break;
                    case 58:
                        PORTC=0b00100000;
                        PORTD=0b00000000;
                        break;
                    case 59:
                        PORTC=0b01000000;
                        PORTD=0b00000000;
                        break;
                    case 0:
                        PORTC=0b10000000;
                        PORTD=0b00000000;
                        break;
                    case 1:
                       PORTC=0b00000000;
                       PORTD=0b00000001;
                       break;
                    case 2:
                      PORTC=0b00000000;
                      PORTD=0b00000010;
                        break;
                    case 3:
                        PORTC=0b00000000;
                        PORTD=0b00000100;
                        break;
                    case 4:
                      PORTC=0b00000000;
                      PORTD=0b00001000;
                        break;
                    case 5:
                      PORTC=0b00000000;
                      PORTD=0b00010000;
                        break;
                    case 6:
                      PORTC=0b00000000;
                      PORTD=0b00100000;
                        break;
                    case 7:
                      PORTC=0b00000000;
                      PORTD=0b01000000;
                        break;
                    default:
                                              
                        break;
                            } // end case
                
            }//end if sec,
            else
            {__delay_us(10); // try first. same time as check all the data,
            
            }
            // contrast = from 0 = night, 1= allmost dark, and 9 = full power.
            if (night==1)
            { __delay_ms(50); // start test night
            
            PORTC=0b00000000;
            PORTD=0b00000000;
            __delay_ms(2150);
            }
            else
            {
                if (contrast==9)__delay_us(2000);
            if (contrast==8)__delay_us(1800);
            if (contrast==7)__delay_us(1600);
            if (contrast==6)__delay_us(1400);
            if (contrast==5)__delay_us(1200);
            if (contrast==4)__delay_us(1000);
            if (contrast==3)__delay_us(800);
            if (contrast==2)__delay_us(600);
            if (contrast==1)__delay_us(400);  // try in test
             if (contrast==0)__delay_us(200);   // night dimmer

             // clear all outputs again.  no need to other transistors,
            PORTC=0b00000000;
            PORTD=0b00000000;
            if (contrast==0)__delay_us(2000);
            if (contrast==1)__delay_us(1800);
            if (contrast==2)__delay_us(1600);
            if (contrast==3)__delay_us(1400);
            if (contrast==4)__delay_us(1200);
            if (contrast==5)__delay_us(1000);
            if (contrast==6)__delay_us(800);
            if (contrast==7)__delay_us(600);
            if (contrast==8)__delay_us(400);
            if (contrast==9)__delay_us(200);
            //total run = 2200 us. 2,2 ms  151 hz. maybee.
            }
          
                  
            
            //---------------------------------------------------------------------
            
            
            if ((minut>52) || (minut<8))
            {
                RB2=0;//sec
               RB0=0;//hour
               RB1=1;//minut
               RB5=1;//outer
                switch (minut) {
                    case 53:
                        PORTC=0b00000001;
                        PORTD=0b00000000;
                        break;
                    case 54:
                        PORTC=0b00000010;
                        PORTD=0b00000000;
                        break;
                    case 55:
                        PORTC=0b00000100;
                        PORTD=0b00000000;
                        break;
                    case 56:
                        PORTC=0b00001000;
                        PORTD=0b00000000;
                        break;
                    case 57:
                        PORTC=0b00010000;
                        PORTD=0b00000000;
                        break;
                    case 58:
                        PORTC=0b00100000;
                        PORTD=0b00000000;
                        break;
                    case 59:
                        PORTC=0b01000000;
                        PORTD=0b00000000;
                        break;
                    case 0:
                        PORTC=0b10000000;
                        PORTD=0b00000000;
                        break;
                    case 1:
                       PORTC=0b00000000;
                       PORTD=0b00000001;
                       break;
                    case 2:
                      PORTC=0b00000000;
                      PORTD=0b00000010;
                        break;
                    case 3:
                        PORTC=0b00000000;
                        PORTD=0b00000100;
                        break;
                    case 4:
                      PORTC=0b00000000;
                      PORTD=0b00001000;
                        break;
                    case 5:
                      PORTC=0b00000000;
                      PORTD=0b00010000;
                        break;
                    case 6:
                      PORTC=0b00000000;
                      PORTD=0b00100000;
                        break;
                    case 7:
                      PORTC=0b00000000;
                      PORTD=0b01000000;
                        break;
                    default:
                                              
                        break;
                            } // end case
                
            }//end if min,
             else
             {__delay_us(10); // try first.
             }
          if (night==1)
            { __delay_ms(50); // start test night
            
            PORTC=0b00000000;
            PORTD=0b00000000;
            __delay_ms(2150);
            }
            else
            {
          // contrast = from 0 = allmost dark, and 9 = full power.
            
            if (contrast==9)__delay_us(2000);
            if (contrast==8)__delay_us(1800);
            if (contrast==7)__delay_us(1600);
            if (contrast==6)__delay_us(1400);
            if (contrast==5)__delay_us(1200);
            if (contrast==4)__delay_us(1000);
            if (contrast==3)__delay_us(800);
            if (contrast==2)__delay_us(600);
            if (contrast==1)__delay_us(400);  // try in test
            if (contrast==0)__delay_us(200);   // night dimmer
            
             // clear all outputs again.  no need to other transistors,
            PORTC=0b00000000;
            PORTD=0b00000000;
            if (contrast==0)__delay_us(2200);
            if (contrast==1)__delay_us(1800);
            if (contrast==2)__delay_us(1600);
            if (contrast==3)__delay_us(1400);
            if (contrast==4)__delay_us(1200);
            if (contrast==5)__delay_us(1000);
            if (contrast==6)__delay_us(800);
            if (contrast==7)__delay_us(600);
            if (contrast==8)__delay_us(400);
            if (contrast==9)__delay_us(200);
            //total run = 2200 us. 2,2 ms  151 hz. maybee.
            }
            
          if ((hour>52) || (hour<8))
            {
              RB2=0;//sec
               RB0=1;//hour
               RB1=0;//minut
               RB5=1;//outer
              
              
              switch (hour) {
                      case 53:
                        PORTC=0b00000001;
                        PORTD=0b00000000;
                        break;
                    case 54:
                        PORTC=0b00000010;
                        PORTD=0b00000000;
                        break;
                    case 55:
                        PORTC=0b00000100;
                        PORTD=0b00000000;
                        break;
                    case 56:
                        PORTC=0b00001000;
                        PORTD=0b00000000;
                        break;
                    case 57:
                        PORTC=0b00010000;
                        PORTD=0b00000000;
                        break;
                    case 58:
                        PORTC=0b00100000;
                        PORTD=0b00000000;
                        break;
                    case 59:
                        PORTC=0b01000000;
                        PORTD=0b00000000;
                        break;
                    case 0:
                        PORTC=0b10000000;
                        PORTD=0b00000000;
                        break;
                    case 1:
                       PORTC=0b00000000;
                       PORTD=0b00000001;
                       break;
                    case 2:
                      PORTC=0b00000000;
                      PORTD=0b00000010;
                        break;
                    case 3:
                        PORTC=0b00000000;
                        PORTD=0b00000100;
                        break;
                    case 4:
                      PORTC=0b00000000;
                      PORTD=0b00001000;
                        break;
                    case 5:
                      PORTC=0b00000000;
                      PORTD=0b00010000;
                        break;
                    case 6:
                      PORTC=0b00000000;
                      PORTD=0b00100000;
                        break;
                    case 7:
                      PORTC=0b00000000;
                      PORTD=0b01000000;
                        break;
                    default:
                                              
                        break;
                            } // end case
                
            }//end if hour,
          else
          {__delay_us(10); // try first.
          }
          if (night==1)
            { __delay_ms(50); // start test night
            
            PORTC=0b00000000;
            PORTD=0b00000000;
            __delay_ms(2150);
            }
            else
            {
        // contrast = from 0= allmost dark, and 9 = full power.
            
            if (contrast==9)__delay_us(2000);
            if (contrast==8)__delay_us(1800);
            if (contrast==7)__delay_us(1600);
            if (contrast==6)__delay_us(1400);
            if (contrast==5)__delay_us(1200);
            if (contrast==4)__delay_us(1000);
            if (contrast==3)__delay_us(800);
            if (contrast==2)__delay_us(600);
            if (contrast==1)__delay_us(400);  // try in test
            if (contrast==0)__delay_us(200);   // night dimmer
            
             // clear all outputs again.  no need to other transistors,
            PORTC=0b00000000;
            PORTD=0b00000000;
            if (contrast==0)__delay_us(2200);
            if (contrast==1)__delay_us(1800);
            if (contrast==2)__delay_us(1600);
            if (contrast==3)__delay_us(1400);
            if (contrast==4)__delay_us(1200);
            if (contrast==5)__delay_us(1000);
            if (contrast==6)__delay_us(800);
            if (contrast==7)__delay_us(600);
            if (contrast==8)__delay_us(400);
            if (contrast==9)__delay_us(200);
            //total run = 2200 us. 2,2 ms  151 hz. maybee.
            }
          
            
        } // end id=0
        }
 
Top