使用C计数器上下进行图片编程

时间:2018-09-04 09:19:16

标签: c pic lcd

#include <xc.h>
#include "LCD.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
    unsigned int a; int x = 0; char s[10000];
    TRISD = 0x00; TRISB = 0x03;
    Lcd_Start();
    while(1)
    {
        Lcd_Clear();Lcd_Set_Cursor(1,1);
        int x = 0;
        if (PORTBbits.RB0==1)
        {
            Lcd_Clear();
            x += 1;
            Lcd_Print_String("Current Number");
            Lcd_Set_Cursor(2,1);
           Lcd_Print_String(x)
        }
        else if(PORTBbits.RB1==1)
        {
            Lcd_Clear();
            x += -1;
            Lcd_Print_String("Current Number");
            Lcd_Set_Cursor(2,1);
            Lcd_Print_String(x);
        }
    }
    return 0;
}

这是Lcd_Print_String

void Lcd_Print_Char(char data)  //Send 8-bits through 4-bit mode
{
    char Lower_Nibble,Upper_Nibble;
    Lower_Nibble = data&0x0F;
    Upper_Nibble = data&0xF0;
    RS = 1;             // => RS = 1
    Lcd_SetBit(Upper_Nibble>>4);             //Send upper half by shifting by 4
    EN = 1;
    for (int i=2130483; i<=0; i--)  NOP(); 
    EN = 0;
    Lcd_SetBit(Lower_Nibble); //Send Lower half
    EN = 1;
    for (int i=2130483; i<=0; i--)  NOP();
    EN = 0;
}

void Lcd_Print_String(char *a)
{
    int i;
    for (i=0;a[i]!='\0';i++)
        Lcd_Print_Char(a[i]);
}

我想在屏幕上显示x的值,我的Lcd_pring_String仅采用字符串类型。有什么方法可以将x转换为字符串,以便我可以在LCD上显示?

我正在使用PIC16f877A,LCD(lm016)和两个开关来获取+ 1,-1的信号。

1 个答案:

答案 0 :(得分:1)

您可能要使用<stdio.h>中声明的sprintf

char buffer[20];             // char buffer
sprintf(buffer, "%d", x);    // "print" x into the char buffer
Lcd_Print_String(buffer);    // send buffer contents to LCD

,或者使用snprintf(如果可用)代替sprintf

snprintf(buffer, sizeof buffer, "%d", x);

我将由此创建一个新功能Lcd_Print_Number