The Godfather talking
You think I am funny guy huh?
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 22, 2024, 11:50:37 11:50


Login with username, password and session length


Pages: 1 [2]  All
Print
Author Topic: volatile variables or eeprom memory  (Read 10655 times)
0 Members and 1 Guest are viewing this topic.
ALLPIC
Active Member
***
Offline Offline

Posts: 114

Thank You
-Given: 64
-Receive: 72


« Reply #25 on: April 26, 2009, 12:57:54 12:57 »

simple way of doing

#include <pic18.h>

#include "delay.h"

unsigned char Counter;

__CONFIG(1,INTIO&FCMDIS&IESODIS);
__CONFIG(2,BORV20&PWRTDIS & WDTEN & WDTPS128);
__CONFIG(3,CCP2RC1 & PBADDIS & MCLREN & LPT1DIS);
__CONFIG(4,DEBUGDIS & LVPDIS & STVREN & XINSTDIS);
__CONFIG(5,CPB);

void main(void)
{
   

   IRCF2 = 0;
   IRCF1 = 0;
   IRCF0 = 1;


   LATB = 0x00;
   LATA = 0x00;
   LATC = 0x00;

    PORTA = 0x00;
    PORTC = 0x00;
   PORTC = 0x00;
    PORTA = 0x00;
    PORTC = 0x00;

    PCFG0 = 1;
    PCFG1 = 1;
    PCFG2 = 1;
    PCFG3 = 1;

    RC0 = 0;
    TRISB = 0x00;
    TRISC = 0x00;
    TRISA = 0x00;
    
    T2CON=0x00;    /* we are testing  TIMER2 */
    T2CKPS1 = 1;
    T2CKPS0 = 1;
    TMR2ON=0;
    TMR2=0;      /*Load initial value to TIMER2*/
    PR2=0xFF;
    TMR2IF=0;    /* Clear overflow flag*/
    TMR2IE=1;

    T1CON=0x00;                                                         /*use TIMER1 as reference for comparison*/
    TMR1IF=0;
    TMR1IE=1;
    TMR1=0;

    T3CON=0x00;                                                         /*use TIMER3 as reference for comparison*/
    TMR3IF=0;
    TMR3IE=1;
    TMR3=0;

    GIEH=0;                                                                 /* allow interrupts from PUSH BUTTONS */
    GIEL=0;

   Counter = eeprom_read(0x00);
   Counter = 0;

   while(1)
   {
      while(RB0 == 0)   //change accoring to your pushbutton switch
      {
         LATB5 ^= 1;      //7 Seg A should toggel that will let you know your PIC is working or not
         DelayMs(250);

      }
      
      Counter++;
      if(Counter>=9)
         Counter = 0;
      eeprom_write(0x00,Counter);
      if(Counter == 0)
      {
         LATA0=1;   LATA1=1;   LATA2=1;   LATA3=0;
             LATB4=1;   LATB5=1;   LATB6=1;   LATB7=0;
      }
      else if(Counter == 1)
      {
         LATA0=0;   LATA1=0;   LATA2=0;   LATA3=0;
             LATB4=1;   LATB5=0;   LATB6=1;   LATB7=0;

      }
      if(Counter == 2)
      {
      
      
      LATA0=1;   LATA1=1;   LATA2=0;   LATA3=1;
      LATB4=1;   LATB5=1;   LATB6=0;   LATB7=0;
      
      }
      
      if(Counter == 3)
      {
      
      LATA0=1;   LATA1=0;   LATA2=0;   LATA3=1;
      LATB4=1;   LATB5=1;   LATB6=1;   LATB7=0;
      
      }
      
      
      if(Counter == 4)
      {
      
      
      LATA0=0;   LATA1=0;   LATA2=1;   LATA3=1;
      LATB4=1;   LATB5=0;   LATB6=1;   LATB7=0;
      
      }
      
      if(Counter == 5)
      {
      
      
      LATA0=1;   LATA1=0;   LATA2=1;   LATA3=1;
      LATB4=0;   LATB5=1;   LATB6=1;   LATB7=0;
      
      }
      
      if(Counter == 6)
      {
      
      LATA0=1;   LATA1=1;   LATA2=1;   LATA3=1;
      LATB4=0;   LATB5=1;   LATB6=1;   LATB7=0;
      
      }
      
      if(Counter == 7)
      {
      
      LATA0=0;   LATA1=0;   LATA2=0;   LATA3=0;
      LATB4=1;   LATB5=1;   LATB6=1;   LATB7=0;
      
      
      }
      
      if(Counter == 8 )
      {
      
      
      LATA0=1;   LATA1=1;   LATA2=1;   LATA3=1;
      LATB4=1;   LATB5=1;   LATB6=1;   LATB7=0;
      
      }
      
      if(Counter == 9)
      {
      
      
      LATA0=1;   LATA1=0;   LATA2=1;   LATA3=1;
      LATB4=1;   LATB5=1;   LATB6=1;   LATB7=0;
      
      }



      

   }

    
}

Posted on: April 26, 2009, 01:54:44 13:54 - Automerged

I checked I think this is working please have a look

Posted on: April 26, 2009, 01:56:15 13:56 - Automerged

I think problem that you need to use LAT rather than Port
Logged
sughoi
Junior Member
**
Offline Offline

Posts: 63

Thank You
-Given: 221
-Receive: 53


know thyself because what else is there to know


« Reply #26 on: April 26, 2009, 01:01:01 13:01 »

This is simple one but still I have to inc. my counter via interrupt. Because my real program is longer than I write and also include some delay and subroutines. So push button must be real time...

Posted on: April 26, 2009, 01:59:00 13:59 - Automerged

ALLPIC can't I add you mikroc program to mine. How can I do?
Logged
sam_des
Senior Member
****
Offline Offline

Posts: 256

Thank You
-Given: 128
-Receive: 151


« Reply #27 on: April 26, 2009, 01:02:31 13:02 »

Hi,

My previous code generates an error with ISIS for EECON1 access being ambigious aboud RD/WR.
I think this is due to mikroC's eeprom library.
I am checking further.... Oh I hate using ready-made libraries..  Angry

Anyway code is ok, if you write your own eeprom access routines.

regards,
sam_des
Logged

Never be afraid to do something new. Remember Amateurs built the Ark, Professionals built the Titanic !
ALLPIC
Active Member
***
Offline Offline

Posts: 114

Thank You
-Given: 64
-Receive: 72


« Reply #28 on: April 26, 2009, 01:07:43 13:07 »

I am sorry I can't able to get this "ALLPIC can't I add you mikroc program to mine. How can I do?"

I recoment you that try to use this code first if you see some number on 7 seg then I will show you how to port this in interrupt
Logged
sughoi
Junior Member
**
Offline Offline

Posts: 63

Thank You
-Given: 221
-Receive: 53


know thyself because what else is there to know


« Reply #29 on: April 26, 2009, 01:13:31 13:13 »

I checked the proteus file and it works properly for simulation. Now I will make additon to my first  code. I think you make some magic in interrupt routine. Because I use V8.2 (not pro version.).
Logged
ALLPIC
Active Member
***
Offline Offline

Posts: 114

Thank You
-Given: 64
-Receive: 72


« Reply #30 on: April 26, 2009, 01:20:11 13:20 »

Ok then I think your problem has been solved.... Great wish you good luck
Logged
sughoi
Junior Member
**
Offline Offline

Posts: 63

Thank You
-Given: 221
-Receive: 53


know thyself because what else is there to know


« Reply #31 on: April 26, 2009, 01:33:47 13:33 »

Still I have problems. In simulation it works but I have to change your code to my hardware.

Posted on: April 26, 2009, 02:24:35 14:24 - Automerged

Beacuse I have to use PIC18F2520 @125kHz, internal oscilator, internal MCLR, and also I have to make it for my hardware. 

Posted on: April 26, 2009, 02:26:53 14:26 - Automerged

At your code I have to change 7 seg port, frequence, TMR0L value and so on.. 
Logged
ALLPIC
Active Member
***
Offline Offline

Posts: 114

Thank You
-Given: 64
-Receive: 72


« Reply #32 on: April 26, 2009, 01:34:06 13:34 »

Internal Oscillator and Internal MCLR problem................

it always give you problem. Try to use external MCLR and internal oscillator. simulation will work but there is no guaranty that hardware will work.... Microchip also not advice this
Logged
sam_des
Senior Member
****
Offline Offline

Posts: 256

Thank You
-Given: 128
-Receive: 151


« Reply #33 on: April 26, 2009, 01:34:53 13:34 »

Hi,

Quote
I checked the proteus file and it works properly for simulation. Now I will make additon to my first  code. I think you make some magic in interrupt routine. Because I use V8.2 (not pro version.).

Nothing magical about it. Compile the code with v8.2 & you will get same results.

Quote
Beacuse I have to use PIC18F2520 @125kHz, internal oscilator, internal MCLR, and also I have to make it for my hardware.

Just change the timer0 intialization. Make sure Timer0 Overflow interrupt occurrs at 10mSec rate.
& that's all... Grin
Changing Switch pin & 7-seg port is just cosmetic change having no influence on code(except unless you use some port which intializes in special mode on power-up like a2d ports).

regards,
sam_des
« Last Edit: April 26, 2009, 01:39:08 13:39 by sam_des » Logged

Never be afraid to do something new. Remember Amateurs built the Ark, Professionals built the Titanic !
sughoi
Junior Member
**
Offline Offline

Posts: 63

Thank You
-Given: 221
-Receive: 53


know thyself because what else is there to know


« Reply #34 on: April 26, 2009, 01:45:23 13:45 »

Ok I see, but now on my computer now proteus, mikroc, mikroc pro and hi-tech are working and also the other programs. I'm confused. Let's make last thing. I will send you my program (my real program). Please help me to make last changes. the code is below..
------------------------------------------------------------------------------------------------------
/*
   This code file was compiled with
   mikroC, mikroElektronika C compiler
   for Microchip PIC microcontrollers
   Version: 8.2.0.0  (Demo Version)
   Note: In Demo version, hex output is limited to 2k of program words.
*/

/*
   Microchip PIC18F2520 @ 125kHZ Internal RC Oscillator
*/

/* Configuration Words  (FUSES)

   CONFIG1:   (Word 1)
   INTIO      0xF8FF  INT RC-Port on RA6,Port on RA7
   FCMDIS     0xBFFF    Fail-Safe Clock Monitor Disabled
   IESODIS    0x7FFF    Internal External Switch Over Mode Disabled
   
   CONFIG2:   (Word 2)
   PWRTDIS    0xFFFF  Power Up Timer Disabled
   BORDIS       0xFFF9  Brown Out Detect Disabled
   WDTDIS       0xFEFF  Watchdog Timer Disabled
   
   CONFIG3:   (Word 3)
   CCP2RC1    0xFFFF  CCP2 Mux RC1
   PBDIGITAL  0xFDFF  PortB A/D Disable, PORTB Digital Port
   LPT1DIS    0xFBFF  Low Power Timer1 Osc Disable
   MCLRDIS    0x7FFF  MCLR Disabled,RE3 Enabled
   
   CONFIG4:   (Word 4)
   XINSTDIS     0xFFBF  Extended CPU Disabled
   STVRDIS     0xFFFE  Stack Overflow Reset Disabled
   LVPDIS         0xFFFB  Low Voltage Program Disabled
   DEBUGDIS     0xFFFF  Background Debug Disabled
   
   CONFIG5:   (Word 5)
   UNPROTECT   0xFFFF  Code Protection Disabled
   
   CONFIG6:   (Word 6)
   UNPROTECT   0xFFFF  Table Write Protection Disabled
   
   CONGFIG7:  (Word 7)
   UNPROTECT   0xFFFF  Table Read Protection Disabled

*/


volatile unsigned char counter;
volatile unsigned char push=1;
unsigned char ky=0;
int syn=0;
#define us100 PORTB.F3
#define svic PORTC.F0

void interrupt()
{

  counter++;
  Delay_ms(150);
  if(counter == 10)
  counter=0;
  while(PORTB.F0=0);
  Delay_ms(20);
  push=1;
  PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=0;
  PORTB.F4=0;   PORTB.F5=0;   PORTB.F6=0;   PORTB.F7=0;
  svic=0;
  us100=0;
  INTCON=0xD0;
}

void sth(int eth)
{
  for(syn=0;syn<eth;syn++)
  {
     if(syn==0)
     PORTB.F7=1;
     if(syn==5)
     PORTB.F7=0;
     if(push == 1)
     break;
     Delay_ms(10);
  }
}

void leds_off()
{
  // a-->RB5 , f-->RB4 , dot-->RB7 , g-->RB6
  // c-->RA0 , e-->RA1 , b-->RA3 , d-->RA2
  /*
    Vss--       --a         ---a---
      d--       --f        d|     |f
      b--       --g         |__b__|
      e--       --dot       |     |
      c--       --Vss      e|     |g
                            ---c---
  */

  PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=0;
  PORTB.F4=0;   PORTB.F5=0;   PORTB.F6=0;   PORTB.F7=0;
}

void pwm_signal(unsigned char j)
{

  for(ky=0;ky<j;ky++)
  {
    if(push == 1)
    break;
    us100=1;
    Delay_us(100);
    if(push == 1)
    break;
    us100=0;
    Delay_us(9900);
  }
}

void main()
{

  INTCON=0x10;
   INTCON2=0x05;
   INTCON3=0xC0;
   PIR1=0x00;
   PIR2=0x00;
   PIE1=0x00;
   PIE2=0x00;
   IPR1=0x00;
   IPR2=0x00;
   RCON=0x80;
    TRISB=0x01;
   TRISC=0x00;
   TRISA=0x00;
   PORTA=0x00;
   PORTB=0x01;
  PORTC=0x00;
  INTCON.GIE=1;
  ADCON0=0x00;
  ADCON1=0x0F;
  ADCON2=0x00;
  OSCCON=0x1E;
  Pwm_Init(20000);
  Pwm_Start();
  Pwm_Change_Duty(128);
  push=1;
  for(;Wink
  {

  if(push==1)
  {
    push=0;
    if(counter == 0)
     {
       //6000 ms toplam periyot, 2000 ms on + 4000 ms off

       PORTA.F0=1;   PORTA.F1=1;   PORTA.F2=1;   PORTA.F3=0;
       PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       svic=0;
       us100=0;
       leds_off();
       pwm_signal(134);
       sth(400);
     }

     if(counter == 1)
     {
       //4990 ms toplam periyot, 1660 ms on + 3330 ms off

       PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=0;
       PORTB.F4=1;   PORTB.F5=0;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       svic=0;
       us100=0;
       leds_off();
       pwm_signal(100);
       sth(333);

     }

     if(counter == 2)
     {
       //4240 ms toplam periyot, 1410 ms on + 2830 ms off

       PORTA.F0=1;   PORTA.F1=1;   PORTA.F2=0;   PORTA.F3=1;
       PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=0;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       svic=0;
       us100=0;
       leds_off();
       pwm_signal(75);
       sth(283);

     }

     if(counter == 3)
     {
       //3990 ms toplam periyot, 1330 ms on + 2660 ms off

       PORTA.F0=1;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=1;
       PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       svic=0;
       us100=0;
       leds_off();
       pwm_signal(67);
       sth(266);

     }

     if(counter == 4)
     {
       //3750 ms toplam periyot, 1250 ms on + 2500 ms off

       PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=1;   PORTA.F3=1;
       PORTB.F4=1;   PORTB.F5=0;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       leds_off();
       svic=0;
       us100=0;
       pwm_signal(59);
       sth(250);

     }

     if(counter == 5)
     {
       //3240 ms toplam periyot, 1080 ms on + 2160 ms off

       PORTA.F0=1;   PORTA.F1=0;   PORTA.F2=1;   PORTA.F3=1;
       PORTB.F4=0;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       leds_off();
       svic=0;
       us100=0;
       pwm_signal(42);
       sth(216);

     }

     if(counter == 6)
     {
        //3000 ms toplam periyot, 1000 ms on + 2000 ms off

        PORTA.F0=1;   PORTA.F1=1;   PORTA.F2=1;   PORTA.F3=1;
        PORTB.F4=0;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

        svic=1;
        pwm_signal(66);
        svic=0;
        us100=0;
        leds_off();
        pwm_signal(34);
        sth(200);
     }

     if(counter == 7)
     {
        //2740 ms toplam periyot, 910 ms on + 1830 ms off

        PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=0;
        PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

        svic=1;
        pwm_signal(66);
        leds_off();
        svic=0;
        us100=0;
        pwm_signal(25);
        sth(183);

      }

      if(counter == Cool
      {
        //2490 ms toplam periyot, 830 ms on + 1660 ms off

        PORTA.F0=1;   PORTA.F1=1;   PORTA.F2=1;   PORTA.F3=1;
        PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

        svic=1;
        pwm_signal(66);
        leds_off();
        svic=0;
        us100=0;
        pwm_signal(17);
        sth(166);

      }

      if(counter == 9)
      {
        //1990 ms toplam periyot, 660 ms on + 1330 ms off

        PORTA.F0=1;   PORTA.F1=0;   PORTA.F2=1;   PORTA.F3=1;
        PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

        svic=1;
        pwm_signal(66);
        leds_off();
        svic=0;
        us100=0;
        sth(133);
       }
       leds_off();
  }

  else

  {
     if(counter == 0)
     {
       //6000 ms toplam periyot, 2000 ms on + 4000 ms off

       svic=1;
       pwm_signal(200);
       svic=0;
       us100=0;
       sth(400);
    }

     if(counter == 1)
     {
       //4990 ms toplam periyot, 1660 ms on + 3330 ms off

       svic=1;
       pwm_signal(166);
       svic=0;
       us100=0;
       sth(333);
     }

     if(counter == 2)
     {
       //4240 ms toplam periyot, 1410 ms on + 2830 ms off

       svic=1;
       pwm_signal(141);
       svic=0;
       us100=0;
       sth(283);
     }

     if(counter == 3)
     {
       //3990 ms toplam periyot, 1330 ms on + 2660 ms off

       svic=1;
       pwm_signal(133);
       svic=0;
       us100=0;
       sth(266);
     }

     if(counter == 4)
     {
       //3750 ms toplam periyot, 1250 ms on + 2500 ms off

       svic=1;
       pwm_signal(125);
       svic=0;
       us100=0;
       sth(250);
     }

     if(counter == 5)
     {
       //3240 ms toplam periyot, 1080 ms on + 2160 ms off

       svic=1;
       pwm_signal(108);
       svic=0;
       us100=0;
       sth(216);
     }

     if(counter == 6)
     {
        //3000 ms toplam periyot, 1000 ms on + 2000 ms off

        svic=1;
        pwm_signal(100);
        svic=0;
        us100=0;
        sth(200);
     }

     if(counter == 7)
     {
        //2740 ms toplam periyot, 910 ms on + 1830 ms off

        svic=1;
        pwm_signal(91);
        svic=0;
        us100=0;
        sth(183);
      }

      if(counter == Cool
      {
        //2490 ms toplam periyot, 830 ms on + 1660 ms off

        svic=1;
        pwm_signal(83);
        svic=0;
        us100=0;
        sth(166);
      }

      if(counter == 9)
      {
        //1990 ms toplam periyot, 660 ms on + 1330 ms off

        svic=1;
        pwm_signal(66);
        svic=0;
        us100=0;
        sth(133);

       }

  }
  }



}
-------------------------------------------------------------------


Last thing for change is writing the value of the counter to EEPROM. Now I just want to add your kernel code file to mine. If I finish this my work will end.

Logged
sam_des
Senior Member
****
Offline Offline

Posts: 256

Thank You
-Given: 128
-Receive: 151


« Reply #35 on: April 26, 2009, 02:10:35 14:10 »

Hi,

Well, I suggest ---
1) You are not using Timer0 for anything, so intialize it to generate periodic interrupt every 10msec.
2) I see that you are using PORTB pin change interrupt for 'svic' & 'us100'. 'counter' depends on which of these ??
3) You can simply avoid using PORTB pin change interrupt & use Timer0 OVF interrupt which will do all the debouncing for you. My code for switch debouncing must be modified for using 2 switches. Here you must decide how you want to do the state machine.
  a) Are simultaneous presses of both switches allowed ?
  b) Do the previous pressed button must be released, before accepting new press ?
  etc..
4) I must again stress that staying in ISR for 170mSec + potential infinite loop is bad, think over it.
5) Writing to eeprom - Decide when to do this  ? Writing to EEPROM on every update of counter( as in my example) is not good. I don't know what your application is, but in my opinion, add some h/w that will signal you about impending power drop. Read PIC's datasheet carefully & check how much power eeprom write requires for reliable write.

Rest of your code can be used unmodified.

That's it ...
Hope that helps.

regards,
sam_des
Logged

Never be afraid to do something new. Remember Amateurs built the Ark, Professionals built the Titanic !
sughoi
Junior Member
**
Offline Offline

Posts: 63

Thank You
-Given: 221
-Receive: 53


know thyself because what else is there to know


« Reply #36 on: April 26, 2009, 02:22:36 14:22 »

Actually I have one switch for interrupt, thank you for your help

Logged
TomJackson69
Active Member
***
Offline Offline

Posts: 218

Thank You
-Given: 26
-Receive: 63


« Reply #37 on: April 26, 2009, 06:36:33 18:36 »

sughoi,

Look at your program I think there are some conflict with interrupt assignment.

You have:
INTCON=0x10;   INT0IE: INT0 External Interrupt Enable bit

INTCON2=0x05;   TMR0IP: TMR0 Overflow Interrupt Priority bit
      RBIP: RB Port Change Interrupt Priority bit

INTCON3=0xC0;   INT2IP: INT2 External Interrupt Priority bit
      INT1IP: INT1 External Interrupt Priority bit

TRISB=0x01;      // you select only pin RB0 as input


In this case, INT1 and INT2 will never trigger because RB1 & RB2 as output.
And PORT B INT on change will not occurse because RB4-RB7 was assigned as output.

INT0 (RB0) will trigger.
TMR0 interrupt will trigger.

So, you have TWO interrupts that will trigger your int routine. Like Sam_des point out before. You have delay in side your int. routine, and it is too long. TMR0 will rollover too fast that mean you will never get out of the int service routine. Also, even you set RB4-RB7 as output, but you are changing it state in the main routine; RB INT on change may set flag for interrupt because LATB (I am not sure on this).

The best way I will try to debug is to remove any delay in the interrupt routine. If interrupt works, then find the source of interrupt. Saving the data to EEPROM or whatever is after all above problem solved.

I would suggest, disable TMR0 INT and other INT for now, enable only INT0 to see if your interrupt routine works. Then move on next and next. That way you know where the problem is.

Saving the data only if it has change it value then use TMR0 INT to check if the value has change it value. If it changed, save it.
How offen do you expect the data will change and on what condition in the real life circuit? The way you program is to increase COUNTER in the ISR and that happen really fast *** in mS ***.

Sughoi, Why don't you listen to ALLPIC and SAM_DES and follow their suggestion? I see ALLPIC change the value for INTCON, INTCON2 and INTCON3 but than you use your. And why don't you SUBMIT your REAL circuit and program so other can help. I am sure it is not value for those helpers.

Tom
« Last Edit: April 26, 2009, 06:50:24 18:50 by TomJackson69 » Logged

Con Rong Chau Tien
sughoi
Junior Member
**
Offline Offline

Posts: 63

Thank You
-Given: 221
-Receive: 53


know thyself because what else is there to know


« Reply #38 on: April 26, 2009, 07:29:29 19:29 »

Ok, I will send the circuit and my code. Actually, if I can write the value of counter to EEPROM and reload it at power on , my problem will solved. So I just want to change my code as a minimum chage. I will follow your instructions. Wait my circuit and source code once. Step by step let's change it. 

Posted on: April 26, 2009, 08:04:02 20:04 - Automerged

I attach my sch file. And also my code file is below. I have to say that, up to now all my hardware and software work properly except writing the counter to EEPROM. This will be my first step and I will follow your instructions.

Please don't be anger. this code will be our starting point. Just tell what to do...

/*
   This code file was compiled with
   mikroC, mikroElektronika C compiler
   for Microchip PIC microcontrollers
   Version: 8.2.0.0  (Demo Version)
   Note: In Demo version, hex output is limited to 2k of program words.
*/

/*
   Microchip PIC18F2520 @ 125kHZ Internal RC Oscillator
*/

/* Configuration Words  (FUSES)

   CONFIG1:   (Word 1)
   INTIO      0xF8FF  INT RC-Port on RA6,Port on RA7
   FCMDIS     0xBFFF    Fail-Safe Clock Monitor Disabled
   IESODIS    0x7FFF    Internal External Switch Over Mode Disabled
   
   CONFIG2:   (Word 2)
   PWRTDIS    0xFFFF  Power Up Timer Disabled
   BORDIS       0xFFF9  Brown Out Detect Disabled
   WDTDIS       0xFEFF  Watchdog Timer Disabled
   
   CONFIG3:   (Word 3)
   CCP2RC1    0xFFFF  CCP2 Mux RC1
   PBDIGITAL  0xFDFF  PortB A/D Disable, PORTB Digital Port
   LPT1DIS    0xFBFF  Low Power Timer1 Osc Disable
   MCLRDIS    0x7FFF  MCLR Disabled,RE3 Enabled
   
   CONFIG4:   (Word 4)
   XINSTDIS     0xFFBF  Extended CPU Disabled
   STVRDIS     0xFFFE  Stack Overflow Reset Disabled
   LVPDIS         0xFFFB  Low Voltage Program Disabled
   DEBUGDIS     0xFFFF  Background Debug Disabled
   
   CONFIG5:   (Word 5)
   UNPROTECT   0xFFFF  Code Protection Disabled
   
   CONFIG6:   (Word 6)
   UNPROTECT   0xFFFF  Table Write Protection Disabled
   
   CONGFIG7:  (Word 7)
   UNPROTECT   0xFFFF  Table Read Protection Disabled

*/


volatile unsigned char counter;
volatile unsigned char push=1;
unsigned char ky=0;
int syn=0;
#define us100 PORTB.F3
#define svic PORTC.F0

void interrupt()
{

  counter++;
  Delay_ms(150);
  if(counter == 10)
  counter=0;
  while(PORTB.F0=0);
  Delay_ms(20);
  push=1;
  PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=0;
  PORTB.F4=0;   PORTB.F5=0;   PORTB.F6=0;   PORTB.F7=0;
  svic=0;
  us100=0;
  INTCON=0xD0;
}

void sth(int eth)
{
  for(syn=0;syn<eth;syn++)
  {
     if(syn==0)
     PORTB.F7=1;
     if(syn==5)
     PORTB.F7=0;
     if(push == 1)
     break;
     Delay_ms(10);
  }
}

void leds_off()
{
  // a-->RB5 , f-->RB4 , dot-->RB7 , g-->RB6
  // c-->RA0 , e-->RA1 , b-->RA3 , d-->RA2
  /*
    Vss--       --a         ---a---
      d--       --f        d|     |f
      b--       --g         |__b__|
      e--       --dot       |     |
      c--       --Vss      e|     |g
                            ---c---
  */

  PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=0;
  PORTB.F4=0;   PORTB.F5=0;   PORTB.F6=0;   PORTB.F7=0;
}

void pwm_signal(unsigned char j)
{

  for(ky=0;ky<j;ky++)
  {
    if(push == 1)
    break;
    us100=1;
    Delay_us(100);
    if(push == 1)
    break;
    us100=0;
    Delay_us(9900);
  }
}

void main()
{

  INTCON=0x10;
   INTCON2=0x05;
   INTCON3=0xC0;
   PIR1=0x00;
   PIR2=0x00;
   PIE1=0x00;
   PIE2=0x00;
   IPR1=0x00;
   IPR2=0x00;
   RCON=0x80;
    TRISB=0x01;
   TRISC=0x00;
   TRISA=0x00;
   PORTA=0x00;
   PORTB=0x01;
  PORTC=0x00;
  INTCON.GIE=1;
  ADCON0=0x00;
  ADCON1=0x0F;
  ADCON2=0x00;
  OSCCON=0x1E;
  Pwm_Init(20000);
  Pwm_Start();
  Pwm_Change_Duty(128);
  push=1;
  for(;Wink
  {

  if(push==1)
  {
    push=0;
    if(counter == 0)
     {
       //6000 ms toplam periyot, 2000 ms on + 4000 ms off

       PORTA.F0=1;   PORTA.F1=1;   PORTA.F2=1;   PORTA.F3=0;
       PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       svic=0;
       us100=0;
       leds_off();
       pwm_signal(134);
       sth(400);
     }

     if(counter == 1)
     {
       //4990 ms toplam periyot, 1660 ms on + 3330 ms off

       PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=0;
       PORTB.F4=1;   PORTB.F5=0;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       svic=0;
       us100=0;
       leds_off();
       pwm_signal(100);
       sth(333);

     }

     if(counter == 2)
     {
       //4240 ms toplam periyot, 1410 ms on + 2830 ms off

       PORTA.F0=1;   PORTA.F1=1;   PORTA.F2=0;   PORTA.F3=1;
       PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=0;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       svic=0;
       us100=0;
       leds_off();
       pwm_signal(75);
       sth(283);

     }

     if(counter == 3)
     {
       //3990 ms toplam periyot, 1330 ms on + 2660 ms off

       PORTA.F0=1;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=1;
       PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       svic=0;
       us100=0;
       leds_off();
       pwm_signal(67);
       sth(266);

     }

     if(counter == 4)
     {
       //3750 ms toplam periyot, 1250 ms on + 2500 ms off

       PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=1;   PORTA.F3=1;
       PORTB.F4=1;   PORTB.F5=0;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       leds_off();
       svic=0;
       us100=0;
       pwm_signal(59);
       sth(250);

     }

     if(counter == 5)
     {
       //3240 ms toplam periyot, 1080 ms on + 2160 ms off

       PORTA.F0=1;   PORTA.F1=0;   PORTA.F2=1;   PORTA.F3=1;
       PORTB.F4=0;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       leds_off();
       svic=0;
       us100=0;
       pwm_signal(42);
       sth(216);

     }

     if(counter == 6)
     {
        //3000 ms toplam periyot, 1000 ms on + 2000 ms off

        PORTA.F0=1;   PORTA.F1=1;   PORTA.F2=1;   PORTA.F3=1;
        PORTB.F4=0;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

        svic=1;
        pwm_signal(66);
        svic=0;
        us100=0;
        leds_off();
        pwm_signal(34);
        sth(200);
     }

     if(counter == 7)
     {
        //2740 ms toplam periyot, 910 ms on + 1830 ms off

        PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=0;
        PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

        svic=1;
        pwm_signal(66);
        leds_off();
        svic=0;
        us100=0;
        pwm_signal(25);
        sth(183);

      }

      if(counter == Cool
      {
        //2490 ms toplam periyot, 830 ms on + 1660 ms off

        PORTA.F0=1;   PORTA.F1=1;   PORTA.F2=1;   PORTA.F3=1;
        PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

        svic=1;
        pwm_signal(66);
        leds_off();
        svic=0;
        us100=0;
        pwm_signal(17);
        sth(166);

      }

      if(counter == 9)
      {
        //1990 ms toplam periyot, 660 ms on + 1330 ms off

        PORTA.F0=1;   PORTA.F1=0;   PORTA.F2=1;   PORTA.F3=1;
        PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

        svic=1;
        pwm_signal(66);
        leds_off();
        svic=0;
        us100=0;
        sth(133);
       }
       leds_off();
  }

  else

  {
     if(counter == 0)
     {
       //6000 ms toplam periyot, 2000 ms on + 4000 ms off

       svic=1;
       pwm_signal(200);
       svic=0;
       us100=0;
       sth(400);
    }

     if(counter == 1)
     {
       //4990 ms toplam periyot, 1660 ms on + 3330 ms off

       svic=1;
       pwm_signal(166);
       svic=0;
       us100=0;
       sth(333);
     }

     if(counter == 2)
     {
       //4240 ms toplam periyot, 1410 ms on + 2830 ms off

       svic=1;
       pwm_signal(141);
       svic=0;
       us100=0;
       sth(283);
     }

     if(counter == 3)
     {
       //3990 ms toplam periyot, 1330 ms on + 2660 ms off

       svic=1;
       pwm_signal(133);
       svic=0;
       us100=0;
       sth(266);
     }

     if(counter == 4)
     {
       //3750 ms toplam periyot, 1250 ms on + 2500 ms off

       svic=1;
       pwm_signal(125);
       svic=0;
       us100=0;
       sth(250);
     }

     if(counter == 5)
     {
       //3240 ms toplam periyot, 1080 ms on + 2160 ms off

       svic=1;
       pwm_signal(108);
       svic=0;
       us100=0;
       sth(216);
     }

     if(counter == 6)
     {
        //3000 ms toplam periyot, 1000 ms on + 2000 ms off

        svic=1;
        pwm_signal(100);
        svic=0;
        us100=0;
        sth(200);
     }

     if(counter == 7)
     {
        //2740 ms toplam periyot, 910 ms on + 1830 ms off

        svic=1;
        pwm_signal(91);
        svic=0;
        us100=0;
        sth(183);
      }

      if(counter == Cool
      {
        //2490 ms toplam periyot, 830 ms on + 1660 ms off

        svic=1;
        pwm_signal(83);
        svic=0;
        us100=0;
        sth(166);
      }

      if(counter == 9)
      {
        //1990 ms toplam periyot, 660 ms on + 1330 ms off

        svic=1;
        pwm_signal(66);
        svic=0;
        us100=0;
        sth(133);

       }

  }
  }



}
Logged
sughoi
Junior Member
**
Offline Offline

Posts: 63

Thank You
-Given: 221
-Receive: 53


know thyself because what else is there to know


« Reply #39 on: April 26, 2009, 09:32:06 21:32 »

It think the problem was solved. But still I make some tests. And I can tell you that mikroC and mikroC Pro are different. Not only the code size but also functionality is different. I spent all my weekend with mikroC but finally I realized something wrong with it. The code compiled by pro verison works while the other does not. Anyway the code is below

#define COUNTER_VAR_ADDRESS             0x0002
volatile unsigned char counter;
volatile unsigned char push=1;
unsigned char ky=0;
int syn=0;
#define us100 PORTB.F3
#define svic PORTC.F0

#define KEY_IDLE                        0
#define KEY_PRES_DEB                    1
#define KEY_REL_WAIT                    2
#define KEY_REL_DEB                     3
unsigned char key_state;

#define KEY_PRESSED                     0
#define KEY_RELEASED                    1

void sth(int eth)
{
  for(syn=0;syn<eth;syn++)
  {
     if(syn==0)
     PORTB.F7=1;
     if(syn==5)
     PORTB.F7=0;
     if(push == 1)
     break;
     Delay_ms(10);
  }
}

void leds_off()
{
  // a-->RB5 , f-->RB4 , dot-->RB7 , g-->RB6
  // c-->RA0 , e-->RA1 , b-->RA3 , d-->RA2
  /*
    Vss--       --a         ---a---
      d--       --f        d|     |f
      b--       --g         |__b__|
      e--       --dot       |     |
      c--       --Vss      e|     |g
                            ---c---
  */

  PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=0;
  PORTB.F4=0;   PORTB.F5=0;   PORTB.F6=0;   PORTB.F7=0;
}

void pwm_signal(unsigned char j)
{

  for(ky=0;ky<j;ky++)
  {
    if(push == 1)
    break;
    us100=1;
    Delay_us(100);
    if(push == 1)
    break;
    us100=0;
    Delay_us(9900);
  }
}

/*
** interrupt()
**
** Accepts: none
** Returns: none
**
** Description:
*/
void interrupt( void )                    // High priority interrupt
{
  if( INTCON.TMR0IF ) {
    INTCON.TMR0IF = 0;                    // Clear flag
    TMR0L         = (255-100-1);          // Reload for next 10mS
   
    if( KEY_PRESSED == PORTC.F1 ) {
    push=1;      // New press
      switch( key_state ) {
        case KEY_IDLE:
          key_state = KEY_PRES_DEB;       // Debounce new press
          break;
        case KEY_PRES_DEB:
          key_state = KEY_REL_WAIT;       // Still pressed, press confirmed
                                          // Wait for release
          if( ++counter >= 10 ) {          // counter++ for 7 seg
            counter = 0;
          }
          EEPROM_Write( COUNTER_VAR_ADDRESS, counter ); // Update EEPROM
          break;
        case KEY_REL_WAIT:
          key_state = KEY_REL_WAIT;       // Still pressed, wait for release
          break;
        default:
          key_state = KEY_IDLE;           // Shouldn't be here, just in case
      }
    } else {                              // No key pressed
      if( KEY_REL_WAIT == key_state ) {   // Just released
        key_state = KEY_REL_DEB;          // Debounce release
      } else {
        key_state = KEY_IDLE;             // Idle
      }
    }
  }
}


void main()
{
  INTCON.GIEL = 0;
  INTCON.GIEH = 0;              // Global interrupt disable
 
  LATB = 0x00;
  TRISB = 0x00;                 // Entire PortB as output
  PORTC.F1 = 1;
  TRISC.F1 = 1;                 // PD7 as input
  TRISA=0;
  PORTA=0;
  ADCON0=0x00;
  ADCON1=0x0F;
  ADCON2=0x00;
  push=1;
  T0CON = 0b01000111;           // Inernal clock, prescalar=256, not on
  TMR0L = (2);          // 10mS @ 10MHz clock
  INTCON.TMR0IF = 0;            // Clear flag
  INTCON.TMR0IE = 1;            // OVF interrupt enabled
  INTCON2.TMR0IP = 1;           // OVF interrupt is HIGH priority
  RCON.IPEN = 1;                // Priority enabled
  T0CON.TMR0ON  = 1;            // Timer0 started
 
  key_state= KEY_IDLE;
 
  counter  = EEPROM_Read( 0x0002 );       // Read from EEPROM on powerup
 
  INTCON.GIEL = 1;
  INTCON.GIEH = 1;              // Global interrupr enable
 
  for( ;; ) {


    if(counter == 0)
     {
       //6000 ms toplam periyot, 2000 ms on + 4000 ms off

       PORTA.F0=1;   PORTA.F1=1;   PORTA.F2=1;   PORTA.F3=0;
       PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       svic=0;
       us100=0;
       leds_off();
       pwm_signal(134);
       sth(400);
     }

     if(counter == 1)
     {
       //4990 ms toplam periyot, 1660 ms on + 3330 ms off

       PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=0;
       PORTB.F4=1;   PORTB.F5=0;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       svic=0;
       us100=0;
       leds_off();
       pwm_signal(100);
       sth(333);

     }

     if(counter == 2)
     {
       //4240 ms toplam periyot, 1410 ms on + 2830 ms off

       PORTA.F0=1;   PORTA.F1=1;   PORTA.F2=0;   PORTA.F3=1;
       PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=0;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       svic=0;
       us100=0;
       leds_off();
       pwm_signal(75);
       sth(283);

     }

     if(counter == 3)
     {
       //3990 ms toplam periyot, 1330 ms on + 2660 ms off

       PORTA.F0=1;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=1;
       PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       svic=0;
       us100=0;
       leds_off();
       pwm_signal(67);
       sth(266);

     }

     if(counter == 4)
     {
       //3750 ms toplam periyot, 1250 ms on + 2500 ms off

       PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=1;   PORTA.F3=1;
       PORTB.F4=1;   PORTB.F5=0;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       leds_off();
       svic=0;
       us100=0;
       pwm_signal(59);
       sth(250);

     }

     if(counter == 5)
     {
       //3240 ms toplam periyot, 1080 ms on + 2160 ms off

       PORTA.F0=1;   PORTA.F1=0;   PORTA.F2=1;   PORTA.F3=1;
       PORTB.F4=0;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

       svic=1;
       pwm_signal(66);
       leds_off();
       svic=0;
       us100=0;
       pwm_signal(42);
       sth(216);

     }

     if(counter == 6)
     {
        //3000 ms toplam periyot, 1000 ms on + 2000 ms off

        PORTA.F0=1;   PORTA.F1=1;   PORTA.F2=1;   PORTA.F3=1;
        PORTB.F4=0;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

        svic=1;
        pwm_signal(66);
        svic=0;
        us100=0;
        leds_off();
        pwm_signal(34);
        sth(200);
     }

     if(counter == 7)
     {
        //2740 ms toplam periyot, 910 ms on + 1830 ms off

        PORTA.F0=0;   PORTA.F1=0;   PORTA.F2=0;   PORTA.F3=0;
        PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

        svic=1;
        pwm_signal(66);
        leds_off();
        svic=0;
        us100=0;
        pwm_signal(25);
        sth(183);

      }

      if(counter == Cool
      {
        //2490 ms toplam periyot, 830 ms on + 1660 ms off

        PORTA.F0=1;   PORTA.F1=1;   PORTA.F2=1;   PORTA.F3=1;
        PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

        svic=1;
        pwm_signal(66);
        leds_off();
        svic=0;
        us100=0;
        pwm_signal(17);
        sth(166);

      }

      if(counter == 9)
      {
        //1990 ms toplam periyot, 660 ms on + 1330 ms off

        PORTA.F0=1;   PORTA.F1=0;   PORTA.F2=1;   PORTA.F3=1;
        PORTB.F4=1;   PORTB.F5=1;   PORTB.F6=1;   PORTB.F7=0;

        svic=1;
        pwm_signal(66);
        leds_off();
        svic=0;
        us100=0;
        sth(133);
       }






  }
}

Logged
oldvan
Senior Member
****
Offline Offline

Posts: 372

Thank You
-Given: 154
-Receive: 107


If the van is a Rockin'...


WWW
« Reply #40 on: April 26, 2009, 09:42:20 21:42 »

Please make use of the CODE tag that is here to make posts containing code more readable.  It only takes a moment, and is a huge improvement.
Logged

Give a man a fish and you feed him for a day.
Teach a man to fish and he will sit around in a boat drinking beer all day.
TomJackson69
Active Member
***
Offline Offline

Posts: 218

Thank You
-Given: 26
-Receive: 63


« Reply #41 on: April 26, 2009, 11:57:54 23:57 »

Sughoi,

I am glad you have use:
INTCON.TMR0IF = 0; // Clear flag
INTCON.TMR0IE = 1; // OVF interrupt enabled
INTCON2.TMR0IP = 1; // OVF interrupt is HIGH priority


Not this:
INTCON=0x10;
INTCON2=0x05;
INTCON3=0xC0;

To make it works.

Anyway, happy to see your project works.

Tom
Logged

Con Rong Chau Tien
sam_des
Senior Member
****
Offline Offline

Posts: 256

Thank You
-Given: 128
-Receive: 151


« Reply #42 on: April 28, 2009, 06:15:55 06:15 »

Hi Sughoi,

Glad to see your problem has been solved.
But...
1) See that you initialized TMR0L=2 in main(), but you kept TMR0L=255-100-1 in interrupt(). Now TMR0 is not overflowing at rate you expect.
2) Whenever you include someone else's or your own code in another project & modify itas per new requirements, make sure you modify the comments accordingly. Othwerwise, your code will confuse you as well as others.

regards,
sam_des
Logged

Never be afraid to do something new. Remember Amateurs built the Ark, Professionals built the Titanic !
Pages: 1 [2]  All
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC