PIC12 random output issue - Page 1
Forum section: electronics / general

author: ECE Misho
author_rank: Newbie
timestamp: June 15, 2015, 08:12:37 pm
content: 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.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?

author: Bruce Abbott
author_rank: Frequent Contributor
timestamp: June 15, 2015, 08:37:26 pm
content: 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?

author: mikerj
author_rank: Super Contributor
timestamp: June 16, 2015, 10:32:14 am
content: 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.

author: Moondeck
author_rank: Regular Contributor
timestamp: June 18, 2015, 11:58:05 am
content: Check connections, get your multimeter out.

author: ECE Misho
author_rank: Newbie
timestamp: June 18, 2015, 05:08:39 pm
content: this is code:Code: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;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

author: ElectricGuy
author_rank: Regular Contributor
timestamp: June 18, 2015, 08:28:29 pm
content: 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 = 0x07Also, 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!

author: Bruce Abbott
author_rank: Frequent Contributor
timestamp: June 18, 2015, 08:33:55 pm
content: 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' isdisabledin '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-1return 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 comparatorGPIO.B4 = 1;while(1){if(GPIO.B3==1){k=rundom (6,seed) ;pattern=hex_value;GPIO=pattern;delay_ms(7000);GPIO=0;}}}

author: ECE Misho
author_rank: Newbie
timestamp: June 18, 2015, 11:41:13 pm
content: thank you all, i really appreciate your helpgonna fix the code and test it asapthanks again