(memory storing) help needed with programming of PIC18F45K20

Discussion in 'Embedded Systems and Microcontrollers' started by reneeqps, Dec 27, 2012.

  1. reneeqps

    Thread Starter New Member

    Dec 27, 2012
    2
    0
    hi guys may i know how can i store the inputs i keyed in and still can display it after i restart the device. for example: i store ABC then i switch off, when i switch on again and press display it will show ABC without having me to key in all over again. now it would reset automatically everytime i restart the device. thank you so much!



    Code ( (Unknown Language)):
    1.  
    2. #include "p18f45K20.h"
    3. #include "delays.h"
    4.  
    5. int Cases = 0;
    6. int Letter = 0;
    7. int Letter2 = 0;
    8. int Store = 0;
    9. int Seq = 0;
    10. int Restore = 0;
    11. int StorageArray[];
    12. /* Delays 4Mhz
    13. Delay1TCY (); // gives a delay of 1 us (implemented as a single NOP instruction).
    14. Delay10TCYx (60); // gives a delay of 10 x 60 x 1us = 600us
    15. Delay100TCYx (60); // gives a delay of 100 x 120 x 1us = 60ms
    16. Delay1KTCYx (60); // gives a delay of 1000 x 240 x 1us = 600ms
    17. Delay10KTCYx (60); // gives a delay of 10000 x 25 x 1us = 6sec
    18. */
    19. //Start of Program ~ flow chart(1)
    20.  
    21. rom char Sequences[26][1] =
    22. {    0x01,//A
    23.     0x02,//B
    24.     0x04,//C
    25.     0x08,//D
    26.     0x10,//E
    27.     0x20,//F
    28.     0x40,//G
    29.     0x80,//H
    30.     0x03,//I
    31.     0x06,//J
    32.     0x0C,//K
    33.     0x18,//L
    34.     0x30,//M
    35.     0x60,//N
    36.     0xC0,//O
    37.     0x07,//P
    38.     0x0E,//Q
    39.     0x1C,//R
    40.     0x38,//S
    41.     0x70,//T
    42.     0xE0,//U
    43.     0x0F,//V
    44.     0x1E,//W
    45.     0x3C,//X
    46.     0x78,//Y
    47.     0xF0,//Z
    48. };
    49. //Declaring All the Words
    50. rom char Characters[52][7] =
    51. {        0x00, 0x00, 0x7F, 0x88, 0x88, 0x88, 0x7F, //A
    52.         0x00, 0x00,    0xFF, 0x91, 0x91, 0x91, 0x6E, //B
    53.         0x00, 0x00, 0x7E, 0x81, 0x81, 0x81, 0x66, //C
    54.         0x00, 0x00, 0xFF, 0x81, 0x81, 0x42, 0x3C, //D
    55.         0x00, 0x00, 0xFF, 0x91, 0x91, 0x91, 0x91, //E
    56.         0x00, 0x00, 0xFF, 0x90, 0x90, 0x90, 0x90, //F
    57.         0x00, 0x00, 0x7E, 0x81, 0x89, 0x89, 0x4E, //G
    58.         0x00, 0x00, 0xFF, 0x10, 0x10, 0x10, 0xFF, //H
    59.         0x00, 0x00, 0x81, 0x81, 0xFF, 0x81, 0x81, //I
    60.         0x00, 0x00, 0x87, 0x81, 0xFF, 0x80, 0x80, //J
    61.         0x00, 0x00, 0xFF, 0x10, 0x28, 0x44, 0x83, //K
    62.         0x00, 0x00, 0xFF, 0x01, 0x01, 0x01, 0x01, //L
    63.         0x00, 0x00, 0x7F, 0x80, 0x7C, 0x80, 0x7F, //M
    64.         0x00, 0xFF, 0x40, 0x30, 0x0C, 0x02, 0xFF, //N
    65.         0x00, 0x00, 0x7E, 0x81, 0x81, 0x81, 0x7F, //O
    66.         0x00, 0x00, 0xFF, 0x88, 0x88, 0x88, 0x60, //P
    67.         0x00, 0x00, 0x7C, 0x82, 0x82, 0x82, 0x7D, //Q
    68.         0x00, 0x00, 0xFF, 0x98, 0x94, 0x92, 0x61, //R
    69.         0x00, 0x00, 0x66, 0x91, 0x91, 0x91, 0x4E, //S
    70.         0x00, 0x00, 0x80, 0x80, 0xFF, 0x80, 0x80, //T
    71.         0x00, 0x00, 0xFF, 0x01, 0x01, 0x01, 0xFF, //U
    72.         0x00, 0x00, 0xF8, 0x06, 0x01, 0x06, 0xF8, //V
    73.         0x00, 0x00, 0xFE, 0x01, 0x1E, 0x01, 0xFE, //W
    74.         0x00, 0x00, 0xC7, 0x28, 0x10, 0x28, 0xC7, //X
    75.         0x00, 0x00, 0xC0, 0x30, 0x0F, 0x30, 0xC0, //Y
    76.         0x00, 0x00, 0x83, 0x85, 0x89, 0x91, 0xE1, //Z
    77.         0x00, 0x00, 0x17, 0x15, 0x15, 0x1F, 0x00, //a
    78.         0x00, 0x00, 0x7F, 0x09, 0x09, 0x09, 0x09, //b
    79.         0x00, 0x00, 0x0E, 0x09, 0x09, 0x09, 0x0A, //c
    80.         0x00, 0x00, 0x0F, 0x09, 0x09, 0x7F, 0x00, //d
    81.         0x00, 0x00, 0x0E, 0x15, 0x15, 0x15, 0x0D, //e
    82.         0x00, 0x00, 0x08, 0x08, 0x7F, 0x48, 0x48, //f
    83.         0x00, 0x00, 0x00, 0x3B, 0x29, 0x29, 0x3F, //g
    84.         0x00, 0x00, 0x00, 0x3F, 0x04, 0x04, 0x07, //h
    85.         0x00, 0x00, 0x00, 0x00, 0x2F, 0x00, 0x00, //i
    86.         0x00, 0x00, 0x03, 0x01, 0x2F, 0x00, 0x00, //j
    87.         0x00, 0x00, 0x3F, 0x08, 0x14, 0x23, 0x3F, //k
    88.         0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, //l
    89.         0x00, 0x00, 0x1F, 0x20, 0x18, 0x20, 0x1F, //m
    90.         0x00, 0x00, 0x3F, 0x20, 0x20, 0x3F, 0x00, //n
    91.         0x00, 0x00, 0x1E, 0x21, 0x21, 0x1E, 0x00, //o
    92.         0x00, 0x00, 0x3F, 0x28, 0x28, 0x38, 0x00, //p
    93.         0x00, 0x00, 0x38, 0x28, 0x28, 0x3F, 0x00, //q
    94.         0x00, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, //r
    95.         0x00 ,0x00, 0x00, 0x1D, 0x15, 0x15, 0x17, //s
    96.         0x00, 0x00, 0x00, 0x08, 0x3F, 0x08, 0x00, //t
    97.         0x00, 0x00, 0x1E, 0x01, 0x01, 0x1E, 0x00, //u
    98.         0x00, 0x00, 0x00, 0x1E, 0x01, 0x1E, 0x00, //v
    99.         0x00, 0x00, 0x1E, 0x01, 0x06, 0x01, 0x1E, //w
    100.         0x00, 0x00, 0x21, 0x12, 0x04, 0x12, 0x21, //x
    101.         0x00, 0x00, 0x00, 0x38, 0x09, 0x09, 0x3F, //y
    102.         0x00, 0x00, 0x00, 0x23, 0x25, 0x29, 0x31  //z
    103. };
    104. void main()
    105. {    
    106. INTCON2bits.RBPU = 0;
    107. TRISB = 0b00000000;
    108. ANSELH = 0b00000000;
    109. TRISD = 0x00;
    110. TRISB = 0xFF;
    111. PORTD = 0x00;
    112. if (PORTBbits.RB0 == 0)
    113. PORTD = 0xFF;
    114. else
    115. PORTD = 0x00;
    116. //Making All Of PORTD Outputs
    117. Wait:
    118. if (PORTBbits.RB3 == 0)
    119. {
    120. WaitA:
    121.     if (PORTBbits.RB3 != 0)
    122.     {
    123.     Start();
    124.     }
    125.     else
    126.     {
    127.     goto WaitA;
    128.     }
    129. }
    130. else
    131. goto Wait;
    132. }
    133.  
    134. int Start()
    135. {
    136. Selections:
    137. switch(Cases)
    138. {
    139. case 0: Letter = 0; goto StartOfProgram;
    140. case 1: Letter = 1; goto StartOfProgram;
    141. case 2: Letter = 2; goto StartOfProgram;
    142. case 3: Letter = 3; goto StartOfProgram;
    143. case 4: Letter = 4; goto StartOfProgram;
    144. case 5: Letter = 5; goto StartOfProgram;
    145. case 6: Letter = 6; goto StartOfProgram;
    146. case 7: Letter = 7; goto StartOfProgram;
    147. case 8: Letter = 8; goto StartOfProgram;
    148. case 9: Letter = 9; goto StartOfProgram;
    149. case 10: Letter = 10; goto StartOfProgram;
    150. case 11: Letter = 11; goto StartOfProgram;
    151. case 12: Letter = 12; goto StartOfProgram;
    152. case 13: Letter = 13; goto StartOfProgram;
    153. case 14: Letter = 14; goto StartOfProgram;
    154. case 15: Letter = 15; goto StartOfProgram;
    155. case 16: Letter = 16; goto StartOfProgram;
    156. case 17: Letter = 17; goto StartOfProgram;
    157. case 18: Letter = 18; goto StartOfProgram;
    158. case 19: Letter = 19; goto StartOfProgram;
    159. case 20: Letter = 20; goto StartOfProgram;
    160. case 21: Letter = 21; goto StartOfProgram;
    161. case 22: Letter = 22; goto StartOfProgram;
    162. case 23: Letter = 23; goto StartOfProgram;
    163. case 24: Letter = 24; goto StartOfProgram;
    164. case 25: Letter = 25; goto StartOfProgram;
    165. }
    166. StartOfProgram:
    167. //Start
    168. PORTD = Sequences[Letter][Letter2];
    169. goto Int1;
    170.  
    171.  
    172. //Int1 Start
    173. Int1:
    174. if (PORTBbits.RB2 == 0)
    175. {
    176. Int1A:
    177.     if (PORTBbits.RB3 != 0)
    178.     {
    179.         if (PORTBbits.RB2 != 0)
    180.         {
    181.             Cases++;
    182.             if (Cases >    25)
    183.             {
    184.             Cases = 0;
    185.             goto Selections;
    186.             }
    187.             else
    188.             goto Selections;
    189.         }
    190.         else
    191.         goto Int1A;
    192.     }
    193.     else if (PORTBbits.RB3 == 0)
    194.     {
    195. Int1A1:
    196.         if (PORTBbits.RB3 != 0)
    197.         {
    198.             if (Store != 0)
    199.             {
    200.             Store--;
    201.             Seq--;
    202.             goto Selections;
    203.             }
    204.         }
    205.         else
    206.         goto Int1A1;
    207.     }
    208. }
    209. else if (PORTBbits.RB3 == 0)
    210. {
    211. Int1B:
    212. if (PORTBbits.RB3 != 0)
    213. {
    214.     Cases --;
    215.     if (Cases < 0)
    216.         {
    217.             Cases = 25;
    218.             goto Selections;
    219.         }
    220.     else
    221.     {
    222.     goto Selections;
    223.     }
    224. }
    225. else
    226. goto Int1B;
    227. }
    228. else if(PORTBbits.RB1 == 0)
    229. {
    230. Int2A:
    231.     if (PORTBbits.RB1 != 0)
    232.     {
    233.         if (PORTBbits.RB3 == 0)
    234.         {
    235.             StorageArray[Store] = Letter +26; //array holding data
    236.             Store++; //array1d value increase
    237.             Seq++; //increase total letter by 1    
    238. Wait5:
    239.             if (PORTBbits.RB3 != 0)
    240.             {
    241.             goto StartOfProgram;
    242.             }
    243.             else
    244.             goto Wait5;
    245.         }
    246.         else
    247.         {    
    248.             StorageArray[Store] = Letter; //array holding data
    249.             Store++; //array1d value increase
    250.             Seq++; //increase total letter by 1    
    251.             goto StartOfProgram;
    252.         }
    253.     }
    254.     else
    255.     goto Int2A;
    256.    
    257. }
    258. else
    259. {
    260. goto Int3A;
    261.  
    262. }
    263. Int3A:
    264. if(PORTBbits.RB0 == 0)
    265. {
    266. Int3B:
    267.     if(PORTBbits.RB0 != 0)
    268.     {
    269.     Restore = Store;
    270.     Display();
    271.     }
    272.     else
    273.     {
    274.     goto Int3B;
    275.     }
    276. }
    277. else
    278. goto StartOfProgram;
    279. }
    280.  
    281. int Display()
    282. {
    283. int x;
    284. int y;
    285. if (Store == 0)
    286. {
    287. Start();
    288. }
    289. if (Store == 1)
    290. {
    291. x = 25;
    292. y = 40;
    293. }
    294. else if (Store == 2)
    295. {
    296. x = 22;
    297. y = 40;
    298. }
    299. else if (Store == 3)
    300. {
    301. x = 15;
    302. y = 10;
    303. }
    304. else if (Store == 4)
    305. {
    306. x = 13;
    307. y = 30;
    308. }
    309. else if (Store > 4)
    310. {
    311. x = 5;
    312. y = 15;
    313. }
    314. //Seq is to allow m to reset to default.
    315.     //Store is to store the letter
    316.     //StorageArray is to store selected
    317. Store = 0;
    318. Repeat1:
    319. //not yet last letter
    320. if (Store != Seq)
    321. {
    322. Letter = StorageArray[Store];
    323. Repeat2:
    324.     if (Letter2 < 8)
    325.     {
    326.     Delay100TCYx (x);
    327.     PORTD = Characters[Letter][Letter2];
    328.     Letter2++;
    329.     goto IntZ;
    330.     }
    331.     else if (Letter2 > 7)
    332.     {
    333.     Store++;
    334.     Letter2 = 0;
    335.     goto IntZ;
    336.     }
    337. }
    338. else
    339. Store = 0;
    340. goto Repeat1;
    341.  
    342. IntZ:
    343. if (PORTBbits.RB3 == 0)
    344. {
    345.  
    346. IntZ1:
    347.     if (PORTBbits.RB3 != 0)
    348.     {
    349.     Letter2 = 0;
    350.     Store = 0;
    351.     Seq = 0;
    352.     Cases = 0;
    353.     Start();
    354.    
    355.     }
    356.     else
    357.     goto IntZ1;
    358. }
    359. else
    360. goto Repeat1;
    361. }
    362.  
    363.  
     
    Last edited by a moderator: Dec 27, 2012
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    You need to store in EPROM. Search your datasheet for EPROM. You don't mention your compiler (always mention your compiler for coding questions) but there should be library functions to store and retrieve EPROM values.

    And please read my signature below.
     
  3. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    Your code looks like C18 so I post you a sample of how to read and write eeprom

    You really don't want to do a bunch of reads and writes to eeprom, Like don't do alot of updates to the same block you can wear it out fast

    Have a look at page 99 of the data sheet to make sure you registers match what is in the code blocks I posted

    Code ( (Unknown Language)):
    1.  
    2. void eeWrite_byte(unsigned char address, unsigned char *_data){
    3.  
    4.     EEDATA = *_data;
    5.     EEADR = address;
    6.     // start write sequence as described in datasheet, page 91
    7.     EECON1bits.EEPGD = 0;
    8.     EECON1bits.CFGS = 0;
    9.     EECON1bits.WREN = 1; // enable writes to data EEPROM
    10.     INTCONbits.GIE = 0;  // disable interrupts
    11.     EECON2 = 0x55;
    12.     EECON2 = 0x0AA;
    13.     EECON1bits.WR = 1;   // start writing
    14.     while(EECON1bits.WR){
    15.         _asm nop _endasm;}
    16.     if(EECON1bits.WRERR){
    17.         printf("ERROR: writing to EEPROM failed!\n");
    18.     }
    19.     EECON1bits.WREN = 0;
    20.     INTCONbits.GIE = 1;  // enable interrupts
    21. }
    22.  
    23. void eeRead_byte(unsigned char address, unsigned char *_data){
    24.     EEADR = address;
    25.     EECON1bits.CFGS = 0;
    26.     EECON1bits.EEPGD = 0;
    27.     EECON1bits.RD = 1;
    28.     *_data = EEDATA;
    29. }
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Since the PIC18F45k20 has no EEPROM it would be a rather futile attempt to attempt to strore anything there. It would be much better to use the flash ROM to persist variables.

    Sorry I don't have a link offhand I'm stuck using an iPad this week.
     
  5. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    What are you talking about read page 99 of the data sheet ErnieM

    http://ww1.microchip.com/downloads/en/devicedoc/41303g.pdf

    There 256 byte of eeprom on that chip

    [​IMG]
     
    Last edited: Dec 28, 2012
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988

    Since when?

    Data EEPROM (bytes)256


    http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en026337
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Ok then I stand corrected... Can't get on mchip where I am. Just get ready as EEPROM is going out of style.
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    To be replaced by????


    Just store in flash?
     
  9. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    There is a new kind of memory it's has better life then eeprom but I don't think it will be in a pic any time soon. MRAM
     
  10. reneeqps

    Thread Starter New Member

    Dec 27, 2012
    2
    0
    hi thank you so much for your help but my lecturer just gave me this project and ask me to do without and guide book or whatsoever. she just gave me the component and all so i'm really lost right now with no knowledge of all these.

    so means i have to type in the above code into my program? thanks
     
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    You need to get a new lecturer. That is horrible advice. Probably the single most important skill for an engineer is to know how to find information. This would be data sheets, instruction manuals, technical papers, other peers..... Pretty much any place information can be found.
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    I would surely hope your lecturer did not intend for you to use a device without looking at it's data sheet.

    When I use code such as this first I will copy from the PDF and paste into my program source (so at most all I need type is corrections to tabs ans spacings), and I will also add a comment noting where I obtained the code from, document and page. This way you are noting the source for future reference and NOT claiming the code as your own work...though in this case any code that does work will be doing the same things in the same order as you are working with hardware.
     
  13. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    EEPROM is for configuration data which must be changed frequently.

    If you need more memory, use serial FLASH.

    Normally data is stored inside the FLASH ROM, where else?
     
  14. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    Serial Flash or EEprom would be better. There life is about the same.

    You mean infrequently, You don't want to update to EEprom every second once a day or so is what you would want.
     
Loading...