1.3.1进程附属例程
来源:互联网 发布:k3软件是什么 编辑:程序博客网 时间:2024/04/28 17:40
(这个是pwm和LED拼凑起来的代码,写的很烂,很烂,就没加备注。总之是很烂的代码。要是初学的话可以看一下,要是但凡会一点的话,就不要在意这些细节了~~~哈哈~~~)
这个是进程的简单应用
一.
本程序实际上是前面几个程序的一个简单的整合程序.用到了pwm列成.LED列成和进程列成,可以锻炼大家对较大程序的理解。(自写)
二.重点掌握函数:
无新函数
三.要求水平
掌握程序原理和结构,之后管道与共享内存都用到了这个程序。
四.由于使用了前面所用到的程序所以没有加备注。
#include <stdio.h>#include <stddef.h>#include <termios.h>#include <unistd.h>#include <stdlib.h>#include <sys/ipc.h>#include <sys/shm.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <sys/types.h>#include"pthread.h"#define PWM_IOCTL_SET_FREQ1#define PWM_IOCTL_STOP0#define ESC_KEY0x1bint temp;static int fd = -1;static void close_buzzer(void);void open_led(int on,int led_on);int led_ons(int leds,char i);void delay(int times);void reader_function();void writer_function();static int getch(void){struct termios oldt,newt;int ch;if (!isatty(STDIN_FILENO)) {fprintf(stderr, "this problem should be run at a terminal\n");exit(1);}// save terminal settingif(tcgetattr(STDIN_FILENO, &oldt) < 0) {perror("save the terminal setting");exit(1);}// set terminal as neednewt = oldt;newt.c_lflag &= ~( ICANON | ECHO );if(tcsetattr(STDIN_FILENO,TCSANOW, &newt) < 0) {perror("set terminal");exit(1);}ch = getchar();// restore termial settingif(tcsetattr(STDIN_FILENO,TCSANOW,&oldt) < 0) {perror("restore the termial setting");exit(1);}return ch;}static void open_buzzer(void){fd = open("/dev/pwm", 0);if (fd < 0) {perror("open pwm_buzzer device");exit(1);}// any function exit call will stop the buzzeratexit(close_buzzer);}static void close_buzzer(void){if (fd >= 0) {ioctl(fd, PWM_IOCTL_STOP);close(fd);fd = -1;}}static void set_buzzer_freq(int freq){// this IOCTL command is the key to set frequencyint ret = ioctl(fd, PWM_IOCTL_SET_FREQ, freq);if(ret < 0) {perror("set the frequency of the buzzer");exit(1);}}static void stop_buzzer(void){int ret = ioctl(fd, PWM_IOCTL_STOP);if(ret < 0) {perror("stop the buzzer");exit(1);}}main(){pthread_t reader;pthread_t writer;pthread_create(&reader,NULL,(void*)&reader_function,NULL);pthread_create(&writer,NULL,(void*)&writer_function,NULL);while(1);}void writer_function(){ int last; int off; int led_on=-1; int f_led; while(1) { off=1; led_on++; if(led_on==4) { led_on=0; } open_led(1,led_on); for(f_led=3;f_led>0;f_led--) { last=led_ons(led_on,off); open_led(0,last); off++; } delay(temp*8); }}void reader_function(){ int freq = 1000 ;open_buzzer();printf( "\nBUZZER TEST ( PWM Control )\n" );printf( "Press +/- to increase/reduce the frequency of the BUZZER\n" ) ;printf( "Press 'ESC' key to Exit this program\n\n" ); while(1) { int key; set_buzzer_freq(freq); temp=((21000-freq)/10); printf( "\tFreq = %d\n", freq ); key = getch(); switch(key) { case '+': if( freq < 20000 ) freq += 10; break; case '-': if( freq > 11 ) freq -= 10 ; break; case ESC_KEY: case EOF: stop_buzzer(); exit(0); default: break; } }}void open_led(int on,int led_on){int ss;ss= open("/dev/leds0", 0);if (ss< 0){ ss= open("/dev/leds", 0);}if (ss< 0){ perror("open device leds"); exit(1);}ioctl(ss, on, led_on);close(ss); //delete????????}int led_ons(int leds,char i){ int a; a=leds; for(i;i>0;i--) { a++; if(a==4) {a=0;} } return a;}void delay(int times){ int i; for(;times>0;times--) for(i=0;i<400;i++);}
0 0
- 1.3.1进程附属例程
- 1.3.2 线程附属例程
- 1.4.1 共享内存附属例程
- 1.5.1 无名管道附属例程
- 1.6.1 有名管道附属
- Perl6 -1.3 子例程1
- java标签(1)自定义及附属功能
- 附属美丽
- 多进程并发编程----进程间传递文件描述符基础~发送接收附属数据函数介绍
- 多进程并发编程----进程间传递文件描述符基础~发送接收附属数据结构体介绍
- linux上编写守护进程的例程
- linux上编写守护进程的例程
- 3.多进程例程:process.c
- linux上编写守护进程的例程
- [Linux]gdb调试多进程多线程例程
- 《Windows程序设计》例程1
- pyQt 例程学习 -1-
- EXCEL例程1
- 记2014“蓝桥杯全国软件大赛"决赛北京之行
- MySQL索引背后的数据结构及算法原理
- Linux FTP自动登陆下载备份,上传文件脚本
- 素朴集合论1.1节习题参考答案
- 斐波那契堆(Fibonacci heaps)
- 1.3.1进程附属例程
- CONCAT(字符串连接函数)和GROUP_CONCAT的一个例子
- Ajax
- POJ 1573 Robot Motion.
- ZOJ-1874
- 图的拓扑排序
- 浙江绿盟科技2011.10.14校招笔试题
- 三种工厂模式的实现
- 中国女性对男士的外表性感有多看重?