简体   繁体   English

我想在MPLAB XC8上获取一些字符,但是不能吗?

[英]I want to get bit of char on MPLAB XC8 but I can't?

My function to get bit is: 我得到的功能是:

extern volatile unsigned char Temp       @ 0x036;
extern volatile __bit W       @ (((unsigned) &Temp)*8) + 4;

void get_bit(volatile unsigned char *reg, unsigned num) {
    W = (*reg & (1 << num));
}

Main function is: 主要功能是:

int main() {
    volatile unsigned char ch = 0b00001000;
    get_bit(&ch, 4);
}

When I compile this code of blocks I get an error (error: expression syntax). 当我编译这些代码块时,我得到一个错误(错误:表达式语法)。

What can I do to fix this problem? 我该怎么做才能解决此问题?

Try this code: 试试这个代码:

#include <stdio.h>

unsigned char get_bit(unsigned char reg, unsigned num) 
{
    return (reg & (1 << num));
}

unsigned char get_bit2(unsigned char reg, unsigned num) 
{
    return (reg & (1 << num))?1:0;
}

int main() 
{
    volatile unsigned char ch = 0b00001000;

    ch |= (1<<4);   // To set bit 4
    printf("%d\n",get_bit(ch, 4)); // If you try on a PC
    printf("%d\n",get_bit2(ch, 4)); // If you try on a PC

    ch &= (~(1<<4));   // To reset bit 4
    printf("%d\n",get_bit(ch, 4)); // If you try on a PC
    printf("%d\n",get_bit2(ch, 4)); // If you try on a PC


    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM