Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

writing random numbers in PIC16F1936 Internal EEPROM

Status
Not open for further replies.

jean12

Advanced Member level 2
Joined
Aug 27, 2013
Messages
529
Helped
5
Reputation
12
Reaction score
6
Trophy points
18
Activity points
5,497
Hello,could anyone advise on how I can write the data randomly in EEPROM using MicroC,I want to refer to Tahmid blog sine table and then create three phase signal chich can control three Phase IGBT system for asynchronous machines.

Please help the data is an array which we write one value by one and then read it back and write to a specific ports(3 pins) outputting 3 signals which control Gates of IGBTs,the frequency has to be variable with an encoder connected on any pin of portb.

Thanks
 

Why not consider using the standard rand() function ?
 

I wonder if Jean12 is asking how to store a table of numbers to be stored in EEPROM so it gets included in the .hex file and is ready to program into a blank device.

I had a look in the MikroC help files and I can't see anything that allows embedded EEPROM data but there are references to .ihex files which may be of use. I don't use that compiler myself but it appears you have to create a new file just for the EEPROM data and program it to the PIC as a separate operation. Someone more familiar with it may know an easier way, loading it into ROM and copying it to EEPROM at run time doesn't seem a good idea.

Brian.
 

Hello,betwixt I just have a sine table comprising around 210 numbers I want to store those in Internal EEPROM of PIC16F1936 and then read them for creating a SPWM on the ECCP pins (3 pins) which I want to use for controlling a three phase IGBT bridge.

Please help.

Thanks
 

Hello,there I am trying here to show what I was thinking in,the following codes are written using Microc,I want to read those values of sien_table[] array and use them for generating an SPWM signal and then get it to the three pins which I use for controlling the IGBT bridge.


The sine table is saved in the EEPROM and then the same values be outputed for creating 3 SPWM gates signals for IGBTs.

Also could you please help me to make the phase shift of 120 degrees for those signals so that I get at the Six IGBT the three phase signal?Or I need to calculate the sine_table according to those angles,with AN889 of microchip I am not understanding how the phase shift has been created,I don`t know assembly language.
The processor in use is 16F1936 the PWM signal is generated with ECCP capabilities;


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
sbit UP at RB1_bit; sbit DOWN at RB2_bit;
 
        unsigned char sine_table[]={123,127,131,133,137, 141, 145, 148, 150, 154, 158, 162, 165, 167,171, 174,177,181,182,186,189,192,195,197,200,203,205,208,209,212,214,216,218,219,222,223,225,227,227,229,230,231,232,233,233,234,235, 235,235,236,236,236,235,235,235,234,233,233,232,231,230,229,227,227,225,223,222,219,218,216,214,212,209,208,205,203,200,197,195,192,189,186,182,181,177,174,171,167,165,162,158,154,150,148,145,141,137,133,131,127,123,119,115,113,109,105,101,98,96,92,88,84,81,79,75,72,69,65,64,60,57,54,51,49,46,43,41,38,37,34,32,30,28,27,24,23,21,19,19,17,16,15,14,13,13,12,11,11,11,10,10,10,11,11,11,12,13,13,14,15,16,17,19,19,21,23,24,27,28,30,32,34,37,38,41,43,46,49,51,54,57,60,64,65,69,72,75,79,81,84,88,92,96,98,101,105,109, 113,115,119,123};
         unsigned short new_DC, newduty;
              char t,ii;
         
         //writing and reading the above data from sine_table those values are used for creating the PWM signal output at the 3 pins of PORTC
         //and then used for ocntrolling the system
         
        /*  writeEEPROM(unsigned char address, unsigned char sine_table[])
{
  unsigned char INTCON_SAVE;//To save INTCON register value
   EEADR = address; //Address to write
   for(t=0;t<=220;t++)
   {
  EEDATL =sine_table[t]; //writing data from the sine table array into memory
 
  EECON1.EEPGD = 0; //Selecting EEPROM Data Memory
  EECON1.WREN = 1; //Enable writing of EEPROM
  INTCON_SAVE=INTCON;//Backup INCON interupt register
  INTCON=0; //Diables the interrupt
  EECON2=0x55; //Required sequence for write to internal EEPROM
  EECON2=0xAA; //Required sequence for write to internal EEPROM
  EECON1.WR = 1; //Initialise write cycle
  INTCON = INTCON_SAVE;//Enables Interrupt
  EECON1.WREN = 0; //To disable write
  while(PIR2.EEIF == 0)//Checking for complition of write operation
  {
  EEADR = address; //Address to be read
  EECON1.EEPGD = 0;//Selecting EEPROM Data Memory
  EECON1.RD = 1; //Initialise read cycle
  return EEDATL; //Returning data
  }
  PIR2.EEIF = 0; //Clearing EEIF bit
}   }
    
               */
               
               
           unsigned write_readEEPROM()
           {
        for (t=0; t<202; t++)
        EEPROM_write(0x80+t,t);
        EEPROM_write(0x02,0XAA);
        EEPROM_write(0x50,0x55);
        delay_us(100);
        for (ii=0;ii<2020;ii++){
        
        CCPR1L=EEPROM_Read(0x80+t);
        
               } }
               
               
    void debounce()
            {
             delay_ms(300);
            }
 void main()
 {
   TRISA=0XFF;
   TRISC=0XF0;
   TRISB=0XF8;
   CCP1CON=0XC;
   PSTR1CON=0X1F;
   PR2=249;
   T2CON=4;
   T2CON.TMR2ON=1;
   T2CON.TMR2ON=1;
 //PWM1CON=    For dead time programming
   PWM1_init(15000);      //Initialise PWM at 15KHZ
while(1){
    new_dc=0;   
       if(!(PORTA.B1)){
      debounce();
      if(new_dc<249)
      new_dc=new_dc+5;   //increment duty cycle
      PORTC.B3=0;
      }
       if(!PORTA.B2){
      debounce();
      if(new_dc!=0)
      new_dc=new_dc-5; //decrement duty cycle
      PORTC.B3=1;
      delay_ms(1000);
           }
           if(newduty!=new_dc)
           {
           newduty=new_DC;
            PWM1_Start();
           PWM1_set_duty(newduty) ;
  
            }
          }
        }



No error noticed in the codes when compiling but I am not getting the output,

check my Microc code here below:




Thank you!!

- - - Updated - - -

Hello andre_teprom,

No,no I created a sine table which I want to store in a EEPROM of PIC16F1936 and the sine table be used for creating SPWM Signal used to trigger an IGBT,not a random number as it is I have calculated values.

Could you please let me know how the phase shift is created in Microc c or ccs when controlling a three phase system?
Thank you
 
Last edited by a moderator:

Hi,

Also could you please help me to make the phase shift of 120 degrees for those signals

120 degree is one third of 360 degree, which is one full wave.
If you have 210 values for a full wave sine, then one third of 210 is 70.

This means while the pointer for phase A runs from 0 to 209
The pointer for phase B runs with an offset of 70. This means from 70...209, 0 ...69
Pointer for phase C: 140...209, 0 ...139

In software something like this:
PointerA = pointerA +1
PointerB = pointerA +70
If pointerB >= 210 then pointerB = pointerB - 210
PointerC = pointerA + 140
If pointerC >= 210 then pointerC = pointerC - 210

Hope this helps
Klaus
 

Could you help me to handle those pointers in MicroC?
I don`t know how to use pointers,could you help me with codes?


If you have worked with the PWM signals in case the look up table is used,is it necessary to configure the Pic FOR PWM functions or we only need to provide the output pin for values received from the EEPROM??

Please help.

Thank you!!
 

There is no reason to store these values at the EEPROM memory. You can keep it statically at the program FLASH memory itself, just as you did at the code above. Keep in mind that the accessing to the data memory usually requires few more time to retrieve the value. In regard to the pointer term that Klauss mentioned, it is just the concept, so that you don't need strictly use a pointer structure to do that. You can perform an indexing shifted by a 70 offset between each output.
 

In case those values are kept in the array as I did in the codes,how to use them so that output on the ports:p1A,P1B,P1C of PIC16F1936 for IGBTs control?

could you help me to handle that case of KlausST by writing corresponding codes in MicroC?Or how the indexing should be than on those 70 values?

Thank you
 

In the code above, you could add the const statement before the table array.
This would ensure that this table would not be stored at the RAM memory, but at the FLASH memory.

Another issue is that I´m not seeing you refer to this table on the code.
Do something like: PWM1_set_duty(sine_table[newduty]) ;
 

Will that statement
PWM1_set_duty(sine_table[newduty]) allow me to make a variable duty cycle?What about phase shifting for three phase lines?could you help me to handle it in Microc or ccs c compiler?

Thank you!!
 

Think of it this way Jean12:

There is only one waveform,
the waveform is created from your table of numbers.
You could use three tables, each containing the same numbers but with them 'rotated' by 1/3 (=120 degree phase shift)
or you could use the same table but pick the values from it at three different places.

The best method is to use one table, declare it as 'const' and pick (using 3 pointers or index values) from it. The 'const' tells the compiler the values will not change, in other words they stay constant so it knows they can be stored in the program memory area instead of RAM.

Example, suppose your array held 180 values, you would start with phase 1 value at sine_table[0], phase 2 at sine_table[60] and phase 3 at sine_table[120]. If instead of using fixed numbers you used a variable 'angle', you could use sine_table[angle], sine_table[angle+60] and sine_table[angle+120] then by increasing 'angle' you move to the next of all three phase values in one move. The only thing you have to be careful of is going beyond the end of the table, if the 'angle' (index or pointer) goes past the end, you have to set it back to the beginning again so it repeatedly cycles all three places through the table. Something like "if (angle >60) angle = 0;"

Brian.
 

Hello Betwixt,unfortunately I am not able to do that in MicroC,the sine table I have has only 201 values which makes only the positive half cycle not a complete cycle,could you please guide me on the sine table values and coding that in MicroC?

Thank you!!
 

Usualy, sine table consist of 90 values for each degree for first quater circle. Other parts calculated based on mirror principle.
One more thing. Sine table and other read only data have to stored in flash. Use 'const' keyword to let compilier where you want to store your data.
 

hi,could you help me to determine those values??The post above was saying that the sine table comprise the values which must be divided into 3 groups for making a 120 degrees phase shift between the phases,could you please help on handling those values and microc respective codes,I was thinking on referring to tahmid blogs but they look not easier to be modified for three phase systems.

See Tahmid Blog:https://tahmidmc.blogspot.com/2012/10/generation-of-sine-wave-using-spwm-in_10.html

Please help.
 

I strongly suggest you make some experiments with one phase to taste the working principle of the PWM module driving the lags of the bridge, and just after add the 2 other phases. After achieving that, you could perform the optimization of the memory usage, scanning its content in the up/down directions instead of recording the full range. As Easyrider mentioned above, there is a symmetry which allows doing that.
 

I strongly suggest you make some experiments with one phase to taste the working principle of the PWM module driving the lags of the bridge, and just after add the 2 other phases. After achieving that, you could perform the optimization of the memory usage, scanning its content in the up/down directions instead of recording the full range. As Easyrider mentioned above, there is a symmetry which allows doing that.

Hi,I know how the Bridge work that is Power electronics but I don`t know how those phase shift could be implemented in MicroC,I now changed the sine table and used the values which should give a complete cycle,what I posted previously is a half cycle[0-180degrees only] now the 33 vales give 0-360 degrees,could you help me to implement the phase shift with MicroC,for power electronics I really know how the all bridge types work.see the current codes below:
PHP:
 sbit UP at RB5_bit; sbit DOWN at RB4_bit;
        const unsigned char sine_table[33] = {0,24,48,69,88,104,115,123,125,123,115,104,88,69,48,24,0,-24,-48,-69,-88,-104,-115,-123,-125,-123,-115,-104,-88,-69,-48,-24,0,};
         unsigned short new_DC, newduty;
              char t,ii;
    void debounce()
            {
             delay_ms(300);
            }
 void main()
 {
   TRISA=0XFF;
   TRISC=0XF0;
   TRISB=0XF8;
   CCP1CON=0XC;
   PSTR1CON=0X1F;
   PR2=249;
   T2CON=4;
   T2CON.TMR2ON=1;
   T2CON.TMR2ON=1;
 //PWM1CON=    For dead time programming
   PWM1_init(15000);      //Initialise PWM at 15KHZ
while(1){
    new_dc=0;   
       if(!(PORTA.B5)){
      debounce();
      if(new_dc<249)
      new_dc=new_dc+5;   //increment duty cycle
      }
       if(!PORTA.B4){
      debounce();
      if(new_dc!=0)
      new_dc=new_dc-5; //decrement duty cycle
           }
           if(newduty!=new_dc)
           {
           newduty=new_DC;
            PWM1_Start();
           PWM1_set_duty(sine_table[newduty]) ;
  
            }
          }
        }

where and how to use the interrupt for phase shifting implementation?

Please help,thank you!!
 

const unsigned char represends unsigned values! They should be in range from 0 to 255.
Anyway, it will blow up. Power electronics is a bad choice for practice. No errors allowed.
 

so what could you advise me for this system? Above you are meaning that I have to say const signed char[]={}!!

Thank you..
 

You stored negative values to the sine table.
I don't know how PWM module will deal with that.

As mentioned above, there is no diference in working with one phase or three phases.
You will just access the sine table shifted from 1/3 or 2/3 of the full range for the reference phase.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top