# Random number using PIC 16F877A

#### ahmedsaleh

Joined Feb 10, 2017
13
code description :
generating 200 random numbers and print it out on the first line of the (16x2) LCD ,while generating numbers if i pressed a push bottom i will save the current random numbe
C:
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End LCD module connections

void main(){
int i,num,num1,yy,x,num2,num3,count;
int num_save[5];
char conv[4];
int txt[5];

TRISB=0;
TRISD=255;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);

for(count=0;count<50;count++){
num=rand();
bytetostr(num,conv);
lcd_out(1,1,conv);
delay_ms(500);
if(PORTD.B0==1){x++; i++ ;
saleh:  if(PORTD.B0==1){goto saleh;}

if(x==1){
num_save[i]=conv;
lcd_out(2,1,num_save[1]);
}

if(x==2){num_save[i]=conv;
lcd_out(2,1,num_save[2]);
}

if(x==3){num_save[i]=conv;
lcd_out(2,1,num_save[3]);
}
}
delay_ms(250);
if(PORTD.B1==1){goto fin;}
}
fin:
Lcd_Cmd(_LCD_CLEAR);

lcd_out(2,1,num_save[1]);
lcd_out(2,1,num_save[2]);
lcd_out(2,1,num_save[3]);

}
r and put it in the second line ,
if i pressed the push bottom for the second time it will save another number and put it down into the second line and then for the third time pressing the push bottom,
after that if i i pushed another bottom it will stop the generation and print the 3 numbers in the LCD but by sorting (ascending or descending ) ,
******i make the code to generate that numbers and saving it but i cant make the code for the second bottom which will print out the 3 numbers ******

#### Attachments

• 118.4 KB Views: 21
Last edited by a moderator:

#### philba

Joined Aug 17, 2017
960
and your question is? Do you want us to write the code for you?

What is the actual assignment?

Finally, let me gently dissuade you from using a goto in your code. Instead use a while loop.

#### GopherT

Joined Nov 23, 2012
8,012
Each line of the LCD displays 16 positions but the same chip is used for devices with up to 40 characters wide by 4 tall.

So, to mice some text down to the second line, you can just send a 0x05 command 40 times to move what is shown in the top row to the bottom row. The characters will remain.

If you want to simply over wright the new number to the second row directly, by adding 40 to the current register.