Author Topic: PIC12 random output issue  (Read 2487 times)

0 Members and 1 Guest are viewing this topic.

Offline ECE MishoTopic starter

  • Newbie
  • Posts: 5
  • Country: england
PIC12 random output issue
« on: June 15, 2015, 08:12:37 pm »
hello,

i've found a code for PIC16 to output a random values, but when trying to implement it to PIC12F629  using MikroC to output a random 3-bit to seven segment display driver....no errors but when trying to run it on breadboard nothing happened.   :palm:

is there something i've done wrong?
is it ever possible to do it using PIC12 or i should switch to another uC ?
also can i use some transistors or any other physical components to perform this task with another PIC12 code?





 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: PIC12 random output issue
« Reply #1 on: June 15, 2015, 08:37:26 pm »
Most likely it's doing 'nothing' because the port pins haven't been set for digital I/O, but it could be something else. Can you show us your code?
   
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3238
  • Country: gb
Re: PIC12 random output issue
« Reply #2 on: June 16, 2015, 10:32:14 am »
We are not mind readers, without showing us the code you have written and some idea of your circuit we have no idea why it isn't working.
 

Offline Moondeck

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: PIC12 random output issue
« Reply #3 on: June 18, 2015, 11:58:05 am »
Check connections, get your multimeter out.
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Offline ECE MishoTopic starter

  • Newbie
  • Posts: 5
  • Country: england
Re: PIC12 random output issue
« Reply #4 on: June 18, 2015, 05:08:39 pm »
this is code:

Code: [Select]
unsigned char rundom(int Limit, int M) {
unsigned char result;
static unsigned int M;
M= (M*32719+3)%32749;
result=((M%Limit)+1);
return result;
}

void main() {
unsigned char k,pattern,seed=1;
unsigned char hex_value[]={0x01,0x02,0x03,0x04,0x05,0x06};
 TRISIO= 0b100000 ;
 GPIO.B4  = 1;
while(1){
if(GPIO.B0==1){
k=rundom (6,seed) ;
pattern=hex_value[k];
GPIO=pattern;
delay_ms(7000);
GPIO=0;
}
}
}

i got it from the internet it's been tested on PIC16 as i mentioned ^^

it output random values to display on 7 segment display
 

Offline ElectricGuy

  • Regular Contributor
  • *
  • Posts: 240
  • Country: pt
Re: PIC12 random output issue
« Reply #5 on: June 18, 2015, 08:28:29 pm »
You have GPIO.B0 ==1 however in your TRISB you have B0 as output, it must be input. ( If this is same kind of input, like a button)
Also, PIC12f629 as Comparators in B0 and B1, you must disable by putting CMOCON = 0x07
Also, that is not random, it will produce same pattern when you power up the pic.

GPIO=pattern - This will probably enter in conflit also, because some of the bits of the GPIO are inputs.

do the changes and see if works!
« Last Edit: June 18, 2015, 08:31:19 pm by ElectricGuy »
Thank you!
Regards
ElectricGuy
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: PIC12 random output issue
« Reply #6 on: June 18, 2015, 08:33:55 pm »
Your code has 3 problems:-

1. The 12F629 has a comparator which is connected to GP0, GP1 and GP2 by default. To use these pins for digital I/O you should disable the comparator by setting CMCON to 7 (comparator off). Without this change GP0 will always read 0 and your program never generates a random number.

2. You are outputting the random number on GP0-2, but also trying to detect a high level (switch?) on GP0. You should move that input to an unused pin. GP3 is the obvious choice because it can only be an input  (also make sure that 'GP3/MCLR pin select' is disabled in 'Edit Project', to enable the GP3 input).

3. You are generating a random number between 1 and 6, but the array hex_value[] requires an index between 0 and 5.  Your code reads one byte past the array (which happens to contain the value 6) so it only returns values from 2 to 6.

Here is the corrected code:-

unsigned char rundom(int Limit, int M) {
unsigned char result;
static unsigned int M;
 M= (M*32719+3)%32749;
 result=((M%Limit)); // random number between 0 and limit-1
 return result;
}

void main() {
unsigned char k,pattern,seed=1;
unsigned char hex_value[]={0x01,0x02,0x03,0x04,0x05,0x06};
 TRISIO   = 0; // all pins are output except GP3 (which is always an input)
 CMCON    = 7; // disable comparator
 GPIO.B4  = 1;
 while(1){
  if(GPIO.B3==1){
   k=rundom (6,seed) ;
   pattern=hex_value[k];
   GPIO=pattern;
   delay_ms(7000);
   GPIO=0;
  }
 }
}

 
     
« Last Edit: June 18, 2015, 08:36:11 pm by Bruce Abbott »
 

Offline ECE MishoTopic starter

  • Newbie
  • Posts: 5
  • Country: england
Re: PIC12 random output issue
« Reply #7 on: June 18, 2015, 11:41:13 pm »
 ^-^

thank you all, i really appreciate your help 

gonna fix the code and test it asap

thanks again
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf