Pic编程使用C计数器上下

问题描述 投票:-1回答:1
#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的信号。

c pic lcd
1个回答
1
投票

你可能想使用在sprintf中声明的<stdio.h>

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

或者代替sprintf使用snprintf(如果有):

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

我要做一个新功能Lcd_Print_Number

© www.soinside.com 2019 - 2024. All rights reserved.