ولكن أتمنى منك ان تشرح لنا كيف يمكننا زيادة العداد لكي يعد الى المليون
|
أخى الكريم
شكرا جزيلا لك
بارك الله فيك
أشكرك على الاستفسار
التعديل للوصول إلى المليون أو ما بعد المليون سهل للغاية وسوف يثبت بعض اسس البرمجة بلغة السى :
فى البداية البرنامج الأول :
كود:
/*
'*******************************************************************************
' Alphanumeric LCD and Push Button.
' Description:
' In this experiment we will work with alphanumeric LCD and push button.
' Communication with LCD will be performed through 4-bits and connections
' is made as follows: D4 with RD, D5 with RD4, D6 with RD6, D7 with RD7;
' RS with RD0 and EN with RD1.
' The Push button is connected to PORT RC0 (for increment) and PORT RC1
' (for decrement). Of course both button have pull-up resistors (4k7).
' To make this project more interesting , you can reach from 0000 to 9999
' by pressing the button.
' Test configuration:
' MCU: PIC16F877A
' SW: MikroC PRO
'*******************************************************************************
*/
// LCD module connections
sbit LCD_RS at RD0_bit; // LCD_RS assigned to PORT RD0;
sbit LCD_EN at RD1_bit; // LCD_EN assigned to PORT RD1;
sbit LCD_D4 at RD4_bit; // LCD_D4 assigned to PORT RD4;
sbit LCD_D5 at RD5_bit; // LCD_D5 assigned to PORT RD5;
sbit LCD_D6 at RD6_bit; // LCD_D6 assigned to PORT RD6;
sbit LCD_D7 at RD7_bit; // LCD_D7 assigned to PORT RD7;
sbit LCD_RS_Direction at TRISD0_bit; // LCD_RS assigned to TRIS D0;
sbit LCD_EN_Direction at TRISD1_bit; // LCD_EN assigned to TRIS D1;
sbit LCD_D4_Direction at TRISD4_bit; // LCD_D4 assigned to TRIS D4;
sbit LCD_D5_Direction at TRISD5_bit; // LCD_D5 assigned to TRIS D5;
sbit LCD_D6_Direction at TRISD6_bit; // LCD_D6 assigned to TRIS D6;
sbit LCD_D7_Direction at TRISD7_bit; // LCD_D7 assigned to TRIS D7;
// End LCD module connections
char Message1[]="COUNTER"; // Message for line1;
unsigned int number = 0;
char *digit = "0000";
void Display_init() // define display_init;
{
digit[0] = number/1000 + 48; // thousands digit;
digit[1] = (number/100)%10 +48; // hundreds digit;
digit[2] = (number/10)%10 + 48; // tens digit;
digit[3] = number%10 +48; // unit digit;
Lcd_Out(2,7,digit); // display on LCD from column 2, character 7;
}
bit oldstate1;
bit oldstate2;
void main() // main;
{
TRISC=0XFF ; // make PORTC as inputs for buttons;
TRISD = 0; // Set PORTD direction to be output;
PORTD = 0; // Clear PORTD;
Lcd_init(); // LCD Initialization;
Lcd_cmd(_LCD_CLEAR); // Clear LCD;
Lcd_cmd(_LCD_CURSOR_OFF); // Cursor mode, off;
Lcd_out(1,2,Message1); // display message1 from column 1, character 2;
while(1) {
if (Button(&PORTC, 0, 1, 1)) { // Button 1 : Detect logical 1
oldstate1 = 1; // Update flag
}
if (oldstate1 && Button(&PORTC, 0, 1, 0)) { // Detect one-to-zero transition
Delay_ms(200); // If button is pressed, delay 0,2s and decrement "number" with 1;
number = number +1;
oldstate1 = 0; // Update flag
}
if(Button(&PORTC,1,1,1)){ // Button 1 : Detect logical 1
oldstate2 = 1; // Update flag
}
if (oldstate2 && Button(&PORTC, 1, 1, 0)) { // Detect one-to-zero transition
Delay_ms(200); // If button is pressed, delay 0,2s and decrement "number" with 1;
number = number -1;
oldstate2 = 0; // Update flag
}
if (number > 9999u) // if it's more than 9999 go to 0;
number = 0;
display_init(); // call display_init();
if(number>10) PORTD.B2=1; // As example
else PORTD.B2=0;
if(number>20) PORTD.B3=1;// As example
else PORTD.B3=0;
} // infinite loop;
}
إذا تتبعنا البرنامج نجد أننا نحتاج إلى تغيير :
1- المتغير
كود:
unsigned int number = 0;
صالح فقط حتى 65535 ومن ثم يجب تغييره إلى المتغير :
كود:
unsigned long number = 0;
وهوصالح حتى 4294967295 .
2- توسعة مدى متغير مصفوفة السلسلة النصية :
كود:
char *digit = "0000";
لتتسع للملايين :
كود:
unsigned long number = 0;
char *digit = "0000000";
3- توسعة أستخراج الخانات من الألوف :
كود:
void Display_init() // define display_init;
{
digit[0] = number/1000 + 48; // thousands digit;
digit[1] = (number/100)%10 +48; // hundreds digit;
digit[2] = (number/10)%10 + 48; // tens digit;
digit[3] = number%10 +48; // unit digit;
Lcd_Out(2,7,digit); // display on LCD from column 2, character 7;
}
لتتسع للملايين :
كود:
000";
void Display_init() // define display_init;
{
/*
digit[0] = number/1000 + 48; // thousands digit;
digit[1] = (number/100)%10 +48; // hundreds digit;
digit[2] = (number/10)%10 + 48; // tens digit;
digit[3] = number%10 +48; // unit digit;*/
digit[0] = number/1000000 + 48;
digit[1] = (number/100000)%10 + 48;
digit[2] = (number/10000)%10 + 48;
digit[3] = (number/1000)%10 +48;
digit[4] = (number/100)%10 +48;
digit[5] = (number/10)%10 +48;
digit[6] = number%10 +48;
Lcd_Out(2,7,digit); // display on LCD from column 2, character 7;
}
هذا كل ما هنالك .
النتائج :
وما زال العد فى ازدياد
البرنامج بعد التعديل :
كود:
/*
'*******************************************************************************
' Alphanumeric LCD and Push Button.
' Description:
' In this experiment we will work with alphanumeric LCD and push button.
' Communication with LCD will be performed through 4-bits and connections
' is made as follows: D4 with RD, D5 with RD4, D6 with RD6, D7 with RD7;
' RS with RD0 and EN with RD1.
' The Push button is connected to PORT RC0 (for increment) and PORT RC1
' (for decrement). Of course both button have pull-up resistors (4k7).
' To make this project more interesting , you can reach from 0000 to 9999
' by pressing the button.
' Test configuration:
' MCU: PIC16F877A
' SW: MikroC PRO
'*******************************************************************************
*/
// LCD module connections
sbit LCD_RS at RD0_bit; // LCD_RS assigned to PORT RD0;
sbit LCD_EN at RD1_bit; // LCD_EN assigned to PORT RD1;
sbit LCD_D4 at RD4_bit; // LCD_D4 assigned to PORT RD4;
sbit LCD_D5 at RD5_bit; // LCD_D5 assigned to PORT RD5;
sbit LCD_D6 at RD6_bit; // LCD_D6 assigned to PORT RD6;
sbit LCD_D7 at RD7_bit; // LCD_D7 assigned to PORT RD7;
sbit LCD_RS_Direction at TRISD0_bit; // LCD_RS assigned to TRIS D0;
sbit LCD_EN_Direction at TRISD1_bit; // LCD_EN assigned to TRIS D1;
sbit LCD_D4_Direction at TRISD4_bit; // LCD_D4 assigned to TRIS D4;
sbit LCD_D5_Direction at TRISD5_bit; // LCD_D5 assigned to TRIS D5;
sbit LCD_D6_Direction at TRISD6_bit; // LCD_D6 assigned to TRIS D6;
sbit LCD_D7_Direction at TRISD7_bit; // LCD_D7 assigned to TRIS D7;
// End LCD module connections
char Message1[]="COUNTER"; // Message for line1;
unsigned long number = 0;
char *digit = "0000000";
void Display_init() // define display_init;
{
/*
digit[0] = number/1000 + 48; // thousands digit;
digit[1] = (number/100)%10 +48; // hundreds digit;
digit[2] = (number/10)%10 + 48; // tens digit;
digit[3] = number%10 +48; // unit digit;*/
digit[0] = number/1000000 + 48;
digit[1] = (number/100000)%10 + 48;
digit[2] = (number/10000)%10 + 48;
digit[3] = (number/1000)%10 +48;
digit[4] = (number/100)%10 +48;
digit[5] = (number/10)%10 +48;
digit[6] = number%10 +48;
Lcd_Out(2,7,digit); // display on LCD from column 2, character 7;
}
bit oldstate1;
bit oldstate2;
void main() // main;
{
TRISC=0XFF ; // make PORTC as inputs for buttons;
TRISD = 0; // Set PORTD direction to be output;
PORTD = 0; // Clear PORTD;
Lcd_init(); // LCD Initialization;
Lcd_cmd(_LCD_CLEAR); // Clear LCD;
Lcd_cmd(_LCD_CURSOR_OFF); // Cursor mode, off;
Lcd_out(1,2,Message1); // display message1 from column 1, character 2;
while(1) {
if (Button(&PORTC, 0, 1, 1)) { // Button 1 : Detect logical 1
oldstate1 = 1; // Update flag
}
if (oldstate1 && Button(&PORTC, 0, 1, 0)) { // Detect one-to-zero transition
Delay_ms(200); // If button is pressed, delay 0,2s and decrement "number" with 1;
number = number +1;
oldstate1 = 0; // Update flag
}
if(Button(&PORTC,1,1,1)){ // Button 1 : Detect logical 1
oldstate2 = 1; // Update flag
}
if (oldstate2 && Button(&PORTC, 1, 1, 0)) { // Detect one-to-zero transition
Delay_ms(200); // If button is pressed, delay 0,2s and decrement "number" with 1;
number = number -1;
oldstate2 = 0; // Update flag
}
if (number > 9999u) // if it's more than 9999 go to 0;
number = 0;
display_init(); // call display_init();
if(number>10) PORTD.B2=1; // As example
else PORTD.B2=0;
if(number>20) PORTD.B3=1;// As example
else PORTD.B3=0;
} // infinite loop;
}