unsigned+int是多少位
基于i.MX6ULL的掉电检测设计与软件测试
基于i.MX6ULL平台设计实现掉电检测功能,首先选择一路IO,利用IO电平变化触发中断,在编写驱动时捕获该路GPIO的中断,然后在中断响应函数中发送信号通知应用程序掉电发生了。
![](https://p0.ssl.img.360kuai.com/t018bf9a9c49d0e35a0.jpg)
图 1.1 掉电信号IO
驱动代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define power_MAJOR 200
static struct class *my_class;
static struct fasync_struct *fasync_queue; //异步通知队列
#define GPIO_NUM 1 //中断引脚为:GPIO1_1
static unsigned int irq_num;
/* 打开 */
int power_open(struct inode *inode,struct file *filp){
return 0;
}
/* 关闭 */
int power_release(struct inode *inode,struct file *filp){
return 0;
}
ssize_t power_read(struct file *filp,char __user *buf,size_t count,loff_t *f_pos){
return count;
}
ssize_t power_write(struct file *file,const char __user *buf,size_t count,loff_t *f_pos){
return count;
}
static int my_fasync(int fd, struct file * filp, int on)
{
int retval;
retval=fasync_helper(fd,filp,on,&fasync_queue);
/*将该设备登记到fasync_queue队列中去*/
if(retval<0)
return retval;
return 0;
}
static const struct file_operations simple_fops={
.owner=THIS_MODULE,
.open=power_open,
.release=power_release,
.read=power_read,
.write=power_write,
.fasync=my_fasync,
};
/* 在中断服务函数中向应用层发送消息-异步通知 */
static irqreturn_t irq_callback (int irqno, void *dev_id){
printk("irq power-detect working !\\n");
if (fasync_queue) {
kill_fasync(&fasync_queue, SIGIO, POLL_IN);
}
return IRQ_HANDLED;
}
int power_init_module(void){
int rtn;
int ret;
/* 注册设备驱动 */
ret = register_chrdev(power_MAJOR,"power-detect-test",&simple_fops);
if(ret<0){
printk("Unable to register character device %d!/n",ret);
return ret;
}
/* 自动创建设备节点 */
my_class = class_create(THIS_MODULE, "my_class");
device_create(my_class, NULL, MKDEV(power_MAJOR, 0), NULL,"powerdetect");
/*gpio申请*/
rtn = gpio_request(GPIO_NUM, "my_irq");
if(rtn!=0){
printk("my_irq irq pin request io failed.\\n");
}
rtn = gpio_direction_input(GPIO_NUM);
if(rtn<0){
printk("gpio_direction_input() failed !\\n");
}
/*获取gpio中断号*/
irq_num = gpio_to_irq(GPIO_NUM);
/*GPIO中断服务函数注册,*/ /*下降沿触发*/
rtn = request_irq(irq_num, irq_callback,IRQF_TRIGGER_FALLING,"my_irq", NULL);
if (rtn<0) {
printk("my_irq request irq false\\n");
} else {
printk("my_irq request irq success: %d\\n",irq_num);
}
printk("module_init sucessful!!!\\n");
return 0;
}
/* 卸载 */
void power_cleanup_module(void){
/* 卸载相应的设备驱动 */
unregister_chrdev(power_MAJOR,"power-detect-test");
device_destroy(my_class,MKDEV(power_MAJOR, 0));
class_destroy(my_class);
/*释放GPIO*/
gpio_free(GPIO_NUM);
printk("module_exit sucessful!!!\\n");
}
/* 宏实现 */
module_init(power_init_module);
module_exit(power_cleanup_module);
/* 开源许可声明 */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Zou");
应用代码:
#include
#include
#include
#include
#include
#include
static int fd;
/* 内核产生异步通知,调用该应用层函数处理 */
void sigterm_handler(int signo)
{
printf("app irq work !!!\\n");
}
int main(void)
{
int oflags;
fd=open("/dev/powerdetect",O_RDWR); //打开设备文件
/* 启动异步通知信号驱动机制 */
signal(SIGIO, sigterm_handler);
fcntl(fd, F_SETOWN, getpid());
oflags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, oflags | FASYNC);
/*建立一个死循环,防止程序结束 */
while(1)
{
printf("sleep\\n");
usleep(200000); //2ms
}
close(fd);
return 0;
}
将驱动编译成模块,上电加载并执行应用程序后,将电压缓慢下调至掉电临界点。触发GPIO下降沿中断,并提供应用程序掉电信号。
![](https://p0.ssl.img.360kuai.com/t01195fed4a9848a267.jpg)
图1..2 掉电检测
","gnid":"9768f9acae436327e","img_data":[{"flag":2,"img":[{"desc":"","height":"534","title":"","url":"https://p0.ssl.img.360kuai.com/t018bf9a9c49d0e35a0.jpg","width":"1286"},{"desc":"","height":"405","title":"","url":"https://p0.ssl.img.360kuai.com/t01195fed4a9848a267.jpg","width":"710"}]}],"original":0,"pat":"art_src_3,fts0,sts0","powerby":"pika","pub_time":1699497000000,"pure":"","rawurl":"http://zm.news.so.com/ebb6a31850ede91b9cadb3bf6ca21a0c","redirect":0,"rptid":"9e628a3909355cbd","rss_ext":[],"s":"t","src":"武汉万象奥科","tag":[],"title":"基于i.MX6ULL的掉电检测设计与软件测试
太宽复4819int 和unsigned int有什么区别?不是都可以表示负数吗? -
都饰览17195557103 ______ int是将一半正数用于表示负数,而unsigned int表示正数范围更大.在不需要负数的情况下要用unsigned int,比如数组下标,用负数没有必要.
太宽复4819单片机中编程为甚么很多都用 unsigned int ,和int有什么区别? -
都饰览17195557103 ______ 区别unsigned int 可以是0~65535 而int可以-32768~35767.为什么要用unsigned int ,原因1、单片机用到了多数是正整数.2、听说unsigned int比int更不...
太宽复4819c++中 unsigned是什么意思 有什么作用 -
都饰览17195557103 ______ 无符号数,作用为存储的数据范围大 unsigned char是无符号字节型,char类型变量的大小通常为1个字节(1字节=8个位),且属于整型.整型的每一种都有无符号(unsigned)和有符号(signed)两种类型(float和double总是带符号的),在...
太宽复4819C语言unsigned int纠错 -
都饰览17195557103 ______ scanf("%u", &userNumEnteries); %u 这个格式 应当用于 无符号整型.signed 声明,表示最高位是符号位,不是数值位.unsigned int, 表示没有符号位,所有的位都是数值位.int -- C 语言中,不是标准的 声明方法.int 可能是short int, 也可...
太宽复4819C语言里只写unsigned,是unsigned int 还是 unsigned long -
都饰览17195557103 ______ unsigned 表示unsigned int;是一种简略写法;VC里边int和long均为4个字节TC里边int为2个字节,long为4个字节
太宽复4819请问c语言中,unsigned int类型变量可以赋给int类型吗???会不会出问题?? -
都饰览17195557103 ______ 可以赋值,正数无所谓,负数会有符号位的问题,赋值后会和原来不一样,是因为负数在系统中是用补码的形式存放的
太宽复4819新手提问(unsigned)后面加个函数什么意思 -
都饰览17195557103 ______ 函数名是一个地址,unsigned是 unsigned int 的简写. 综上,我的理解是:将void break_point(void)函数的地址强制转换为unsigned int并赋值给pISR_EINT0. over
太宽复4819unsigned long跟unsigned int都是32字节,有什么区别 -
都饰览17195557103 ______ 默认为unsigned int. 这是C语言的一种缺省规则.即当定义变量 unsigned a; 时,与定义 unsigned int a; 是完全相同的.而要定义unsigned long,则必须写全unsigned long所有文字,如 unsigned long b; 但是在32位编译器中,int和long都是占4个字节,unsigned int和unsigned long并没有区别.
太宽复4819unsigned int与int的数据运算后的结果 -
都饰览17195557103 ______ unsigned int 和int型进行运算,int型要转为unsigned int,运算结果为unsigned int.a=6b=-20转成unsigned int 16位编译器下就是65536-20=65516,所以a+b>6,所以c=1.返回值为1.
太宽复4819如何将unsigned char型转换成int型 -
都饰览17195557103 ______ #include int main() { unsigned char ch='5'; printf("%d\n",ch-'0');//ch的ASCII值为48+5,所以减去48(即字符'0')即可.printf("%d\n",(int)ch-48);//强转(int)ch完后,ch成为int型的53.//因为53才是ch真正在计算机中存储的值.return 0; } 其实,你可以将字符ch='5',看做ch=53,这是它的本质.所以,ch是可以直接和数字相加减的.