Mplab pic program is not working - pic

I am trying to make a smart home automation device where when u enter the room the lights automatically turn on and vice versa I use for my project two lasers to specify whether people enter or leave the room i also want the pic to count how many people entered the room and turn the lights off after the last one that enter the room leave to do this i use pic16f877a ic and mplab as my ide with xc8 compiler , so far the program works so good but it doesn't count the people who entered the room but turn off the lights only after one leaves which is not cool and i don't really know where the problem is my code looks alright. here is the code:
/*
* File: main.c
* Author: Fady
*
* Created on September 23, 2014, 9:56 PM
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 4000000
#define laser1 PORTBbits.RB0
#define laser2 PORTBbits.RB1
#define isOn ==0
#define isOff ==1
int x1 = 0;
int x2 = 0;
int main() {
char people = 0;
nRBPU = 0;
TRISBbits.TRISB0 = 1; //laser 1 for input
TRISBbits.TRISB0 = 1; //laser 2 for input
TRISCbits.TRISC0 = 0; //output LED for output
PORTCbits.RC0 = 0;
while(1){
beginning:
if(( laser1 isOn && laser2 isOn ) || ( laser1 isOff && laser2 isOff )){ //if both lasers are on
goto beginning;
}else if(laser1 isOff && laser2 isOn){ //if laser1 is off and laser2 is on
readO:
if(laser2 isOff){
if(people == 0){
people = 1;
PORTCbits.RC0 = 1;
}if(people >= 1){
people++;
}
}else{
for(; x2 <= 1000; x2++){
__delay_ms(1);
goto readO;
}
}
x2 = 0;
}else if(laser1 isOn && laser2 isOff){ //if laser1 is on and laser2 is off
readC:
if(laser1 isOff){
people--;
if(people == 0){
PORTCbits.RC0 = 0;
}
}else{
for(; x1 <= 1000; x1++){
__delay_ms(1);
goto readC;
}
}
x1 = 0;
}
}
}
i think it has no problem but i dont know what is wrong here anyways thanks in advance for any replier who is gonna solve this it would really be a great help for me

First try to reduce a code in an understandable form.
1st: Avoid jumping operations like goto:
2nd: Too many if statements with unclear results, try to use SWITCH statement.
beginning:
if(( laser1 isOn && laser2 isOn ) || ( laser1 isOff && laser2 isOff )){ //if both lasers are on
goto beginning;
why doing this?
You are in forever loop!

ok, let's keep things simple.
countPeople = 0;
while(1)
{
if(PORTBbits.RB0 == 0) // People enter
{
countPeople++; //
//TODO: some thing if you want
}
elseif(PORTBbits.RB0 == 1) // people out or something like this
{
countPeople--;
}
//if nothing happend
}

Related

pic18F47J53 RTCC not starting

Using a PIC18F47J53, MPLAB x, XC8 (v2.31), I am trying to use the internal RTCC, but I cannot see it counting (the seconds). The register RTCVALL, is not changing after waiting a few seconds.
I would like to use the 8MHz internal oscillator for the main clock, and the internal INTRC for the RTCC. Maybe first someone can confirm is it possible to use those 2?
The code is fairly simple, just setting a "seconds" value in RTCVALL, wait a bit, read the same register and hoping to find it has changed.. but it hasn't.
I am posting the main part of the code.
Other question, what is the RTCC pin is supposed to output? I have chosen the "seconds" as output, but if it is supposed to toggle high/low every seconds, where can I see in the datasheet the duty cycle? in my case the LED on RTCC pin stays solid high.
'''
// CONFIG1L
#pragma config WDTEN = OFF // Watchdog Timer (Disabled - Controlled by SWDTEN bit)
#pragma config PLLDIV = 1 // PLL Prescaler Selection (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CFGPLLEN = OFF // PLL Enable Configuration Bit (PLL Disabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset (Enabled)
#pragma config XINST = OFF // Extended Instruction Set (Disabled)
// CONFIG1H
#pragma config CPUDIV = OSC1 // CPU System Clock Postscaler (No CPU system clock divide)
#pragma config CP0 = OFF // Code Protect (Program memory is not code-protected)
// CONFIG2L
#pragma config OSC = INTOSC // Oscillator (INTOSC)
#pragma config SOSCSEL = HIGH // T1OSC/SOSC Power Selection Bits (High Power T1OSC/SOSC circuit selected)
#pragma config CLKOEC = ON // EC Clock Out Enable Bit (CLKO output enabled on the RA6 pin)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor (Enabled)
#pragma config IESO = ON // Internal External Oscillator Switch Over Mode (Enabled)
// CONFIG2H
#pragma config WDTPS = 32768 // Watchdog Postscaler (1:32768)
// CONFIG3L
#pragma config DSWDTOSC = INTOSCREF// DSWDT Clock Select (DSWDT uses INTRC)
#pragma config RTCOSC = INTOSCREF // RTCC Clock Select (INTRC)
#pragma config DSBOREN = ON // Deep Sleep BOR (Enabled)
#pragma config DSWDTEN = ON // Deep Sleep Watchdog Timer (Enabled)
#pragma config DSWDTPS = G2 // Deep Sleep Watchdog Postscaler (1:2,147,483,648 (25.7 days))
// CONFIG3H
#pragma config IOL1WAY = ON // IOLOCK One-Way Set Enable bit (The IOLOCK bit (PPSCON<0>) can be set once)
#pragma config ADCSEL = BIT10 // ADC 10 or 12 Bit Select (10 - Bit ADC Enabled)
#pragma config MSSP7B_EN = MSK7 // MSSP address masking (7 Bit address masking mode)
// CONFIG4L
#pragma config WPFP = PAGE_127 // Write/Erase Protect Page Start/End Location (Write Protect Program Flash Page 127)
#pragma config WPCFG = OFF // Write/Erase Protect Configuration Region (Configuration Words page not erase/write-protected)
// CONFIG4H
#pragma config WPDIS = OFF // Write Protect Disable bit (WPFP<6:0>/WPEND region ignored)
#pragma config WPEND = PAGE_WPFP// Write/Erase Protect Region Select bit (valid when WPDIS = 0) (Pages WPFP<6:0> through Configuration Words erase/write protected)
#pragma config LS48MHZ = SYS48X8// Low Speed USB mode with 48 MHz system clock bit (System clock at 48 MHz USB CLKEN divide-by is set to 8)
#define UINT_TO_BCD_ONES(x) ((x) % 10)
#define UINT_TO_BCD_TENS(x) (((x) % 100) / 10)
#define BCD_TO_UINT_TENS(x) ((x) >> 4)
#define BCD_TO_UINT_ONES(x) ((x) & 0x0F)
void main(void)
{
// set to 8MHz internal oscillator
OSCCON = 0x70;
// not sure that is necessary but either 0 or 1 don't work
OSCTUNEbits.INTSRC = 1;
pic_uart1_init(UART_BDS_9600);
__delay_ms(1000);
uint8_t seconds = 0;
// seconds RTCC output pin
PADCFG1bits.RTSECSEL1 = 0;
PADCFG1bits.RTSECSEL0 = 1;
pic_rtc_set_alarm_output(ON);
pic_rtc_enable(ON);
pic_rtc_wr_enable(ON);
pic_rtc_set_seconds(45);
pic_rtc_wr_enable(OFF);
// wait some time
for (int i = 0; i < 12; i++){
__delay_ms(1000);
printf("-> RTCVALL = %d\n", RTCVALL);
printf("-> RTCVALH = %d\n", RTCVALH);
}
pic_rtc_read_seconds(&seconds);
pic_rtc_enable(OFF);
printf("seconds = %d\n", seconds);
}
pic_status_t pic_rtc_enable(feature_status_t set_status)
{
pic_rtc_wr_enable(ON);
RTCCFGbits.RTCEN = set_status;
pic_rtc_wr_enable(OFF);
return PIC_SUCCESS;
}
pic_status_t pic_rtc_set_alarm_output(feature_status_t set_status)
{
RTCCFGbits.RTCOE = set_status;
return PIC_SUCCESS;
}
pic_status_t pic_rtc_wr_enable(feature_status_t set_status)
{
INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0xAA;
RTCCFGbits.RTCWREN = set_status;
INTCONbits.GIE = 1;
return PIC_SUCCESS;
}
pic_status_t pic_rtc_read_seconds(uint8_t *seconds)
{
// point to minutes
RTCCFGbits.RTCPTR1 = 0;
RTCCFGbits.RTCPTR0 = 0;
printf("RTCCFGbits.RTCPTR1 = %d\n", RTCCFGbits.RTCPTR1);
printf("RTCCFGbits.RTCPTR0 = %d\n", RTCCFGbits.RTCPTR0);
printf("RTCCFGbits.RTCWREN = %d\n", RTCCFGbits.RTCWREN);
uint8_t buffer_rd_sec = RTCVALL;
printf("buffer_rd_sec (BCD) = 0x%02x\n", buffer_rd_sec);
*seconds = (uint8_t)((BCD_TO_UINT_TENS(buffer_rd_sec) * 10) + (BCD_TO_UINT_ONES(buffer_rd_sec)));
return PIC_SUCCESS;
}
pic_status_t pic_rtc_set_seconds(uint8_t seconds)
{
if (seconds > 59)
return PIC_FAIL;
// point to seconds
RTCCFGbits.RTCPTR1 = 0;
RTCCFGbits.RTCPTR0 = 0;
printf("RTCCFGbits.RTCPTR1 = %d\n", RTCCFGbits.RTCPTR1);
printf("RTCCFGbits.RTCPTR0 = %d\n", RTCCFGbits.RTCPTR0);
printf("RTCCFGbits.RTCWREN = %d\n", RTCCFGbits.RTCWREN);
uint8_t buf_ones = UINT_TO_BCD_ONES(seconds);
printf("buf_ones = 0x%02x\n", buf_ones);
uint8_t buf_tens = UINT_TO_BCD_TENS(seconds);
printf("buf_tens = 0x%02x\n", buf_tens);
uint8_t buffer = buf_tens << 4 | buf_ones;
printf("buffer = 0x%02x\n", buffer);
RTCVALL = buffer;
return PIC_SUCCESS;
}
'''
Thanks for having a look and help
I got it working, looks like it wasn't the understanding of the PIC the problem, but the XC8 compiler instead..
I thought the RTCWREN bit had to be checked before enable the RTCEN
pic_status_t pic_rtc_enable(feature_status_t set_status)
{
while(!RTCCFGbits.RTCWREN); // wait for the bit to be set
RTCCFGbits.RTCEN = set_status;
return PIC_SUCCESS;
}
and found that the bit wasn't set at all, resulting the rest of the code been ignored.
so I looked into my pic_rtc_wr_enable function and I am very puzzled why the parameter set_status is making the block!
it is an enum,
typedef enum {
OFF = 0,
ON = 1,
} feature_status_t;
but if you replace the code with
pic_status_t pic_rtc_wr_enable(feature_status_t set_status)
{
if (set_status == 1) {
INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0xAA;
RTCCFGbits.RTCWREN = 1;
INTCONbits.GIE = 1;
}
return PIC_SUCCESS;
}
then it works perfectly fine.
So why XC8 doesn't substitute my enum ON with its int value of 1 for the bit assignment? despite it works in the check?
Any ideas?
For the RTCC pin, got it working too now, and it looks like the pin out is pulsing every seconds (in my case), and duty cycle is 50%.

SDCC integer comparison unexpected behavior

I'm trying to get started on a project using a PIC18F24J10. Attempting to use SDCC for this. At this point I've reduced my code to simply trying to nail down what is happening, as I've been seeing bizarre behavior for a while now and can't really proceed until I figure out what is going on. Not sure if this is my only problem at this point, but I have no idea why this is happening. Timer interrupt fires off, coupled with a #defined for loop causes LEDs on PORTC to blink maybe twice a second. If I just do a PORTC=0xFF and PORTC=0 this works fine. But it gets weird when I try to go much beyond that.
...
#define _RC0 31
#define _RC1 32
#define _RC2 33
#define _RC3 34
#define _RC4 35
#define _RC5 36
#define _RC6 37
#define _RC7 38
#define HI 1
#define LO 0
void why(unsigned char p, int z)
{
if(z == HI)
{
if(p == _RC0) PORTCbits.RC0 = 1;
else if(p == _RC1) PORTCbits.RC1 = 1;
else if(p == _RC2) PORTCbits.RC2 = 1;
else if(p == _RC3) PORTCbits.RC3 = 1;
else if(p == _RC4) PORTCbits.RC4 = 1;
else if(p == _RC5) PORTCbits.RC5 = 1;
else if(p == _RC6) PORTCbits.RC6 = 1;
else if(p == _RC7) PORTCbits.RC7 = 1;
}
else if(z == LO)
{
PORTC = 0b11110000;
}
else
{
PORTC = 0b10101010;
}
}
void timer_isr (void) __interrupt(1) __using (1)
{
TMR0H=0;
TMR0L=0;
why(_RC0, LO);
why(_RC1, LO);
why(_RC2, LO);
WAIT_CYCLES(5000);
why(_RC0, HI);
why(_RC1, HI);
why(_RC2, HI);
WAIT_CYCLES(5000);
}
void main(void)
{
WDTCONbits.SWDTE = 0;
WDTCONbits.SWDTEN = 0;
TRISC = 0b00000000;
PORTC=0b00000000;
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IE = 1;
T0CONbits.T08BIT = 0;
T0CONbits.T0CS = 0;
T0CONbits.PSA = 1;
TMR0H = 0;
TMR0L = 0;
T0CONbits.TMR0ON = 1;
while(1)
{
}
}
In the code above, four of the LEDs should blink, while the other four stay on. Instead, the LEDs stay on in the 10101010 pattern that happens in the "else" block, which should happen when "why" is called with any value other than HI or LO. I never call it with anything else, so why does it ever reach that?
UPDATE - Further reduction, no more interrupts or unspecified includes/defines. This is now the entirety of the program, and I am still seeing the same behavior. Changed the pattern from 10101010 to 10101011 so that I could verify the chip is actually being programmed with the new code, and it appears to be.
#include "pic16/pic18f24j10.h"
#define WAIT_CYCLES(A) for(__delay_cycle = 0;__delay_cycle < A;__delay_cycle++)
int __delay_cycle;
#define HI 1
#define LO 0
void why(int z)
{
if(z == HI)
{
PORTC = 0b11111111;
}
else if(z == LO)
{
PORTC = 0b11110000;
}
else
{
PORTC = 0b10101011;
}
}
void main(void)
{
TRISC = 0b00000000;
PORTC=0b00000000;
while(1)
{
why(LO);
WAIT_CYCLES(5000);
why(HI);
WAIT_CYCLES(5000);
}
}
Once the interrupt is asserted it is never cleared. That results in timer_isr() being called repeatedly. No other code is ever reached. The TMR0IF bit must be cleared in software by the Interrupt Service Routine.
Keep in mind you not only need to spend less time in the ISR than the period of the timer - it’s a good practice to spend the minimum amount of time necessary.
Remove the delays and simply toggle a flag or increment a register. In your main while (1) loop monitor the flag or counter and make your calls to why() from there.

MPLAB X IDE ADC code not working

i am a beginner to microcontroller and I just got into ADC but whenever I try to do a conversion it never works, I am trying to display the result on an LCD but the problem is not the LCD's because I tried its code alone and it worked so the problem is definitely the ADC's registers, here is the full code::
main.c:
#include "config.h"
int result;
void main(){
TRISAbits.TRISA0 = 1; //Set Port A for input
ADCON0 = 0b01000000; //Configuring ADCON0 register
ADCON1 = 0b10000000; //Configuring ADCON1 register
ADCON0bits.ADON = 1; //Turn on ADON bit in ADCON0 register to turn on ADC module
__delay_us(50); //Delay for the capacitor to be charged
ADCON0bits.GO_DONE = 1;
while(ADCON0bits.GO_DONE == 1);
result = ADRESH && ADRESL;
initLCD();
write_character(result);
while(1);
}
config.h:
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 4000000
void send_command(int command);
void write_character(int character);
void enable_blink(void);
void moveto(char b6, char b5, char b4, char b3, char b2, char b1, char b0);
void initLCD(void);
void send_command(int command){
PORTCbits.RC0 = 0;
PORTB = command;
enable_blink();
}
void write_character(int character){
PORTCbits.RC0 = 1;
PORTB = character;
enable_blink();
}
void enable_blink(){
PORTCbits.RC1 = 1;
__delay_ms(10);
PORTCbits.RC1 = 0;
__delay_ms(10);
}
void moveto(char b6, char b5, char b4, char b3, char b2, char b1, char b0){
PORTCbits.RC0 = 0;
PORTBbits.RB7 = 1;
PORTBbits.RB6 = b6;
PORTBbits.RB5 = b5;
PORTBbits.RB4 = b4;
PORTBbits.RB3 = b3;
PORTBbits.RB2 = b2;
PORTBbits.RB1 = b1;
PORTBbits.RB0 = b0;
enable_blink();
}
void initLCD(){
TRISB = 0;
TRISC = 0;
send_command(0x38);
__delay_us(40);
send_command(0x01);
__delay_ms(1.75);
send_command(0x0C);
__delay_us(40);
}
You may have a problem with your result line. Try the following:
result = ( ( ADRESH << 8 ) | ( ADRESL ) )
The double & operator ( && ) is the logical AND. AND will return true ( 1 ) as long as ADRESH and ADRESL are both greater than 1. You want the bitwise OR ( | ) operator to combine two bytes into one int, with the ADRESH as the most-significant-byte and the ADRESL as the least-significant-byte.
Hopefully this works!

PIC USART code not working

I have covered lately adc and lcd in microcontroller and gone into USART and as usual my first code I make is not working and I need some help with discovering the problem, here's the code:
Transmitter code:
main.c:
#include "config.h"
void main(){
TRISCbits.TRISC6 = 1;
TRISCbits.TRISC7 = 1;
TRISDbits.TRISD0 = 1;
SPBRG = 25;
TXSTAbits.TX9 = 0;
TXSTAbits.SYNC = 0;
TXSTAbits.BRGH = 1;
TXSTAbits.TXEN = 1;
RCSTAbits.SPEN = 1;
RCSTAbits.RX9 = 0;
RCSTAbits.CREN = 0;
while(1){
while(TRMT == 0);
if(PORTDbits.RD0 == 1){
TXREG = 0xFF;
}else{
TXREG = 0;
}
}
}
Receiver code:
main.c:
#include "config.h"
char recieve;
void main(){
TRISCbits.TRISC6 = 1;
TRISCbits.TRISC7 = 1;
TRISDbits.TRISD0 = 0;
PORTDbits.RD0 = 0;
SPBRG = 25;
TXSTAbits.TX9 = 0;
TXSTAbits.SYNC = 0;
TXSTAbits.BRGH = 1;
TXSTAbits.TXEN = 1;
RCSTAbits.SPEN = 1;
RCSTAbits.RX9 = 0;
RCSTAbits.CREN = 0;
while(1){
RCREG = recieve;
if(recieve == 0xFF){
PORTDbits.RD0 = 1;
}else{
PORTDbits.RD0 = 0;
}
}
}
and for both transmitter and receiver projects config.h is a header file where I set the frequency of crystal oscillator and configuration bits so it is the same file/code for both projects
config.h:
/*
* File: config.h
* Author: Fady
*
* Created on August 25, 2014, 1:53 PM
*/
// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 4000000
EDIT
I am trying to communicate 2 pic microcontrollers in which if I press the button on D0 in transmitter it sends a 0xFF data and then the receiver checks for if the received data == 0xFF if yes it turns on the LED on D0 in receiver but when I connect it on isis the TX pin of receiver keeps blinking high and low (red and blue) each half a second but when I press the button the high (red) signal keeps for a bit longer but keeps flashing with high and low and I suppose this is for start bit but the receiver doesn't turn on the led that's the error part I don't know what's wrong here
Its just a very stupid silly c programming mistake I wrote:
RCREG = recieve;
which will take the value of receive and assign it to RCREG instead it should be
receive = RCREG;
which will assign the value of RCREG to receive and worked after building...

pwm value not changing

I have written a pwm code for Atmega128. I am using fast pwm mode with non-inverting pulse on compare match and I need to change the OCR0 value at certain times. Yet it doesn't change. Anyone knows what is the problem here ??
#include <avr/interrupt.h>
#include <avr/io.h>
uint8_t tick_1sec;
void timer1_init(void) // 1 second timer
{
OCR1A = 15624;
TIMSK |= (1<<OCIE1A);
TCCR1B = (1<<WGM12); //CTC mode
TCCR1B |= (1<<CS12)|(0<<CS11)|(1<<CS10);
}
ISR(TIMER1_COMPA_vect) //1 second interrupt
{
cli();
tick_1sec = 1;
sei();
}
void timer0_init(void) // fast pwm with OC0 non-inverting mode
{
TCCR0 = (1<<FOC0)|(1<<WGM01)|(1<<WGM00);
TCCR0 |= (1<<COM01)|(0<<COM00);
TCCR0 |= (1<<CS02)|(1<<CS01)|(1<<CS00);
OCR0 = 63;
TIMSK |= (1<<OCIE0);
}
int main(void)
{
uint8_t t = 0;
DDRB = 0xFF;
timer0_init();
timer1_init();
sei();
while(1){
if (tick_1sec)
{
tick_1sec = 0;
t++;
if (t == 10){
OCR0 = 127;
}
else if (t == 20){
OCR0 = 191;
}
else if (t == 30){
OCR0 = 63;
t = 0;
}
}
}
return 0;
}
Things to check:
I recommend declaring tick_1sec as volatile to prevent the compiler of hyper-optimizing that register.
What is your clock frequency? Your ISR will deliver 1s calls only if your CPU frequency is 16MHz (==> 16.000.000 / 1024 / 15624)
You might have a LED in your hardware which you can invert from a) the ISR b) within the first if () in main to see if this is ever reached.
update: "volatile"
The link provided by #skyrift in his comment is very worth reading.
When you use Atmel Studio, compile your code once with/without the volatile keyword and compare what the compiler is doing ==> Solution explorer / Output Files / *.lss ... you will see each C statement and how the compiler converts it to machine code ... an exercise worth once in a while when working with micros ...

Resources