daeunlee

upload all labs

No preview for this file type
#include<wiringPi.h>
#define LED_ON 1
#define LED_OFF 0
const int Led[16]={4,17,18,27,22,23,24,25,6,12,13,16,19,20,26,21};
void LEDControl(int pinnum){
int i;
for(i=0;i<16;i++){
if(i==pinnum)
digitalWrite(Led[i],LED_ON);
else
digitalWrite(Led[i],LED_OFF);
}
}
int main(void){
int i;
if(wiringPiSetupGpio()==-1)
return 1;
for(i=0;i<16;i++){
pinMode(Led[i],OUTPUT);
digitalWrite(Led[i],LED_OFF);
}
while(1){
for(i=0;i<16;i+=2){
LEDControl(i);
delay(500);
}
for(i=1;i<16;i+=2){
LEDControl(i);
delay(500);
}
}
return 0;
}
No preview for this file type
#include<wiringPi.h>
#define LED_ON 1
#define LED_OFF 0
const int Led[16]={4,17,18,27,22,23,24,25,6,12,13,16,19,20,26,21};
void LEDControl(int pinnum){
int i;
for(i=0;i<16;i++){
if(i==pinnum || i==pinnum+1)
digitalWrite(Led[i],LED_ON);
else
digitalWrite(Led[i],LED_OFF);
}
}
int main(void){
int i;
if(wiringPiSetupGpio()==-1)
return 1;
for(i=0;i<16;i++){
pinMode(Led[i],OUTPUT);
digitalWrite(Led[i],LED_OFF);
}
while(1){
for(i=0;i<16;i+=2){
LEDControl(i);
delay(500);
}
}
return 0;
}
No preview for this file type
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#define GPIO_BASE 0x3F200000
#define GPFSEL1 0x04
#define GPSET0 0x1C
#define GPCLR0 0x28
int main() {
int fd = open( "/dev/mem", O_RDWR|O_SYNC );
if ( fd < 0 ){
printf( "can't open /dev/mem \n" );
exit(-1);
}
char *gpio_memory_map
= (char *)mmap(0, 4096, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, GPIO_BASE );
if ( gpio_memory_map == MAP_FAILED ) {
printf( " Error : mmap \n" );
exit(-1);
}
volatile unsigned int* gpio
= (volatile unsigned int*)gpio_memory_map;
gpio[GPFSEL1/4] = (1<<24);
int i;
for ( i=0; i<5; i++ ) {
gpio[GPCLR0/4] = (1<<18);
sleep(1);
gpio[GPSET0/4] = (1<<18);
sleep(1);
}
munmap( gpio_memory_map, 4096);
return 0;
}
No preview for this file type
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#define GPIO_BASE 0x3F200000
#define GPFSEL1 0x04
#define GPSET0 0x1C
#define GPCLR0 0x28
int main() {
int fd = open( "/dev/mem", O_RDWR|O_SYNC );
if ( fd < 0 ){
printf( "can't open /dev/mem \n" );
exit(-1);
}
char *gpio_memory_map
= (char *)mmap(0, 4096, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, GPIO_BASE );
if ( gpio_memory_map == MAP_FAILED ) {
printf( " Error : mmap \n" );
exit(-1);
}
volatile unsigned int* gpio
= (volatile unsigned int*)gpio_memory_map;
gpio[GPFSEL1/4] = (1<<18);
int i;
for ( i=0; i<5; i++ ) {
gpio[GPCLR0/4] = (1<<16);
sleep(1);
gpio[GPSET0/4] = (1<<16);
sleep(1);
}
munmap( gpio_memory_map, 4096);
return 0;
}
No preview for this file type
#include<wiringPi.h>
#define LED_ON 1
#define LED_OFF 0
const int Led[16]={4,17,18,27,22,23,24,25,6,12,13,16,19,20,26,21};
void LEDControl(int pinnum){
int i;
for(i=0;i<16;i++){
if(i==pinnum)
digitalWrite(Led[i],LED_ON);
else
digitalWrite(Led[i],LED_OFF);
}
}
int main(void){
int i;
if(wiringPiSetupGpio()==-1)
return 1;
for(i=0;i<16;i++){
pinMode(Led[i],OUTPUT);
digitalWrite(Led[i],LED_OFF);
}
while(1){
for(i=0;i<16;i++){
LEDControl(i);
delay(500);
}
}
return 0;
}
No preview for this file type
#include <wiringPi.h>
const int LedRed[8] = { 4, 17, 18, 27, 22, 23, 24, 25 };
const int Keypad[8] = { 16, 13, 12, 6, 21, 26, 20, 19 };
int LedON[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
int lastKey[8] = { 1, 1, 1, 1, 1, 1, 1, 1 };
int KeypadRead() {
int i, keypadnum=-1;
for(i=0; i<8; i++) {
if(!digitalRead(Keypad[i]) && lastKey[i]==1) {
keypadnum = i;
lastKey[i]=0;
break;
}
else if(digitalRead(Keypad[i]) && lastKey[i]==0)
lastKey[i]=1;
}
return keypadnum;
}
void LedControl(int keypadnum) {
int i;
for(i=0; i<8; i++) {
if(i==keypadnum) {
LedON[i]=!LedON[i];
digitalWrite(LedRed[i], LedON[i]);}
}
}
int main(void) {
int i, keypadnum=-1;
if(wiringPiSetupGpio() == -1)
return 1;
for(i=0; i<8; i++) {
pinMode(LedRed[i], OUTPUT);
digitalWrite(LedRed[i], LedON[i]);
}
for(i=0; i<8; i++)
pinMode(Keypad[i], INPUT);
while(1) {
keypadnum = KeypadRead();
LedControl(keypadnum);
}
return 0;
}
No preview for this file type
#include <wiringPi.h>
const int LedRed[8] = { 4, 17, 18, 27, 22, 23, 24, 25 };
const int Keypad[5] = { 6, 12, 13, 16, 19 };
int state=0;
void checkKey3(){
if(KeypadRead()==2)
state=0;
}
int KeypadRead() {
int i, keypadnum=-1;
for(i=0; i<8; i++) {
if(!digitalRead(Keypad[i])) {
keypadnum = i;
break;
}
}
return keypadnum;
}
void LedControl(int num) {
int i;
for(i=0; i<8; i++) {
checkKey3();
if(state==0)
return;
if(i==num)
digitalWrite(LedRed[i], HIGH);
else
digitalWrite(LedRed[i], LOW);
}
}
void LedON(int order){
int i;
if(order==1){
for(i=0; i<8; i++) {
LedControl(i);
if(state==0)
return;
delay(500);
}
LedControl(-1);
}
else if (order==-1){
for(i=7; i>=0; i--) {
LedControl(i);
if(state==0)
return;
delay(500);
}
LedControl(-1);
}
}
int main(void) {
int i, keypadnum=-1;
if(wiringPiSetupGpio() == -1)
return 1;
for(i=0; i<8; i++) {
pinMode(LedRed[i], OUTPUT);
digitalWrite(LedRed[i], LOW);
}
for(i=0; i<5; i++)
pinMode(Keypad[i], INPUT);
while(1) {
state=0;
keypadnum = KeypadRead();
if(keypadnum== 3) {
state=1;
LedON(1);
}
else if(keypadnum== 4) {
state=1;
LedON(-1);
}
else if(keypadnum== 0){
while(1){
state=1;
LedON(1);
if(state==0)
break;
}
}
else if(keypadnum== 1){
while(1){
state=1;
LedON(-1);
if(state==0)
break;
}
}
}
return 0;
}
No preview for this file type
#include <wiringPi.h>
const int LedRed[8] = { 4, 17, 18, 27, 22, 23, 24, 25 };
const int Keypad[8] = { 16, 13, 12, 6, 21, 26, 20, 19 };
int KeypadRead() {
int i, keypadnum=-1;
for(i=0; i<8; i++) {
if(!digitalRead(Keypad[i])) {
keypadnum = i;
break;
}
}
return keypadnum;
}
void LedControl(int keypadnum) {
int i;
for(i=0; i<8; i++) {
if(i==keypadnum)
digitalWrite(LedRed[i], HIGH);
else
digitalWrite(LedRed[i], LOW);
}
}
int main(void) {
int i, keypadnum=-1;
if(wiringPiSetupGpio() == -1)
return 1;
for(i=0; i<8; i++) {
pinMode(LedRed[i], OUTPUT);
digitalWrite(LedRed[i], LOW);
}
for(i=0; i<8; i++)
pinMode(Keypad[i], INPUT);
while(1) {
keypadnum = KeypadRead();
LedControl(keypadnum);
}
return 0;
}
\ No newline at end of file
No preview for this file type
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
const int FndSelectPin[6] = { 4, 17, 18, 27, 22, 23 };
const int FndPin[8] = { 6, 12, 13, 16, 19, 20, 26, 21 };
const int FndFont[6] = { 0x00, 0x76, 0x79, 0x38, 0x38, 0x3F };
void init(){
int i;
if( wiringPiSetupGpio() == -1 ){
printf( "wiringPiSetupGpio() error\n" );
exit(-1);
}
for(i=0;i<6;i++){
pinMode( FndSelectPin[i], OUTPUT );
digitalWrite( FndSelectPin[i], HIGH );
}
for(i=0;i<8;i++){
pinMode( FndPin[i], OUTPUT );
digitalWrite( FndPin[i], LOW );
}
}
void FndSelect(int position){
int i;
for(i=0;i<6;i++){
if(i==position){
digitalWrite(FndSelectPin[i],LOW);
}
else{
digitalWrite(FndSelectPin[i],HIGH);
}
}
}
void FndDisplay(int position, int num){
int i;
int flag=0;
int shift=0x01;
for(i=0;i<8;i++){
flag=(FndFont[num]&shift);
digitalWrite(FndPin[i], flag);
shift<<=1;
}
FndSelect(position);
}
int main(){
int pos;
int data[6]={5,4,3,2,1,0};
init();
int i=0;
unsigned long prevtime=millis();
while(1){
unsigned long curtime=millis();
if(curtime-prevtime > 500){
i+=1;
i=i%6;
prevtime=curtime;
}
for(pos=0;pos<6;pos++){
FndDisplay((pos+i)%6,data[pos]);
delay(1);
}
}
return 0;
}
\ No newline at end of file
No preview for this file type
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
const int FndSelectPin[6] = { 4, 17, 18, 27, 22, 23 };
const int FndPin[8] = { 6, 12, 13, 16, 19, 20, 26, 21 };
const int FndFont[10] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66,
0x6D, 0x7D, 0x07, 0x7F, 0x67 };
void init(){
int i;
if( wiringPiSetupGpio() == -1 ){
printf( "wiringPiSetupGpio() error\n" );
exit(-1);
}
for(i=0;i<6;i++){
pinMode( FndSelectPin[i], OUTPUT );
digitalWrite( FndSelectPin[i], HIGH );
}
for(i=0;i<8;i++){
pinMode( FndPin[i], OUTPUT );
digitalWrite( FndPin[i], LOW );
}
}
void FndSelect(int position){
int i;
for(i=0;i<6;i++){
if(i==position){
digitalWrite(FndSelectPin[i],LOW);
}
else{
digitalWrite(FndSelectPin[i],HIGH);
}
}
}
void FndDisplay(int position, int num){
int i,number;
int flag=0;
int shift=0x01;
number=FndFont[num];
if(position==2)
number|=0x80;
for(i=0;i<8;i++){
flag=(number&shift);
digitalWrite(FndPin[i], flag);
shift<<=1;
}
FndSelect(position);
}
int main(){
int pos;
int time=0;
init();
unsigned long prevtime=millis();
while(1){
unsigned long curtime=millis();
if(curtime-prevtime > 10){
prevtime=curtime;
time++;
}
int data[6]={time%10, (time%100)/10, (time%1000)/100,
(time/1000)%10, (time/10000)%10, time/100000};
for(pos=0;pos<6;pos++){
FndDisplay(pos,data[pos]);
delay(1);
}
}
return 0;
}
\ No newline at end of file
No preview for this file type
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
const int FndSelectPin[6] = { 4, 17, 18, 27, 22, 23 };
const int FndPin[8] = { 6, 12, 13, 16, 19, 20, 26, 21 };
const int FndFont[10] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66,
0x6D, 0x7D, 0x07, 0x7F, 0x67 };
void init(){
int i;
if( wiringPiSetupGpio() == -1 ){
printf( "wiringPiSetupGpio() error\n" );
exit(-1);
}
for(i=0;i<6;i++){
pinMode( FndSelectPin[i], OUTPUT );
digitalWrite( FndSelectPin[i], HIGH );
}
for(i=0;i<8;i++){
pinMode( FndPin[i], OUTPUT );
digitalWrite( FndPin[i], LOW );
}
}
void FndDisplay(int position, int num){
int i;
int flag=0;
int shift=0x01;
for(i=0;i<8;i++){
flag=(FndFont[num]&shift);
digitalWrite(FndPin[i], flag);
shift<<=1;
}
digitalWrite( FndSelectPin[position], LOW);
}
int main(int argc, char **argv){
if(argc != 3){
printf("Usage: %s [position][number]",argv[0]);
exit(-1);
}
init();
FndDisplay(atoi(argv[1]), atoi(argv[2]));
return 0;
}
\ No newline at end of file
No preview for this file type
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
const int FndSelectPin[6] = { 4, 17, 18, 27, 22, 23 };
const int FndPin[8] = { 6, 12, 13, 16, 19, 20, 26, 21 };
const int FndFont[10] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66,
0x6D, 0x7D, 0x07, 0x7F, 0x67 };
void init(){
int i;
if( wiringPiSetupGpio() == -1 ){
printf( "wiringPiSetupGpio() error\n" );
exit(-1);
}
for(i=0;i<6;i++){
pinMode( FndSelectPin[i], OUTPUT );
digitalWrite( FndSelectPin[i], HIGH );
}
for(i=0;i<8;i++){
pinMode( FndPin[i], OUTPUT );
digitalWrite( FndPin[i], LOW );
}
}
void FndSelect(int position){
int i;
for(i=0;i<6;i++){
if(i==position){
digitalWrite(FndSelectPin[i],LOW);
}
else{
digitalWrite(FndSelectPin[i],HIGH);
}
}
}
void FndDisplay(int position, int num){
int i;
int flag=0;
int shift=0x01;
for(i=0;i<8;i++){
flag=(FndFont[num]&shift);
digitalWrite(FndPin[i], flag);
shift<<=1;
}
FndSelect(position);
}
int main(){
int pos;
int data[6]={0,1,2,3,4,5};
init();
while(1){
for(pos=0;pos<6;pos++){
FndDisplay(pos,data[pos]);
delay(1);
}
}
return 0;
}
\ No newline at end of file
No preview for this file type
#include <wiringPi.h>
#include <softPwm.h>
#define MOTOR_MT_N_PIN 17
#define MOTOR_MT_P_PIN 4
#define LEFT_ROTATE 1
#define RIGHT_ROTATE 2
void MotorStop() {
softPwmWrite(MOTOR_MT_N_PIN, 0);
softPwmWrite(MOTOR_MT_P_PIN, 0);
}
void MotorControl(int rotate, int speed) {
if (rotate == LEFT_ROTATE) {
digitalWrite(MOTOR_MT_P_PIN, LOW);
softPwmWrite(MOTOR_MT_N_PIN, speed);
}
else if (rotate == RIGHT_ROTATE) {
digitalWrite(MOTOR_MT_N_PIN, LOW);
softPwmWrite(MOTOR_MT_P_PIN, speed);
}
}
int main(void){
if (wiringPiSetupGpio() == -1)
return 1;
pinMode(MOTOR_MT_N_PIN, OUTPUT);
pinMode(MOTOR_MT_P_PIN, OUTPUT);
softPwmCreate(MOTOR_MT_N_PIN, 0, 128);
softPwmCreate(MOTOR_MT_P_PIN, 0, 128);
int i;
for(i=1;i<5;i++){
MotorControl(LEFT_ROTATE,i*32);
delay(2000);
MotorStop();
delay(2000);
}
for(i=4;i>0;i--){
MotorControl(RIGHT_ROTATE,i*32);
delay(2000);
MotorStop();
delay(2000);
}
return 0;
}
No preview for this file type
#include <wiringPi.h>
#include <softPwm.h>
#define LedPin 22
int main(void){
if (wiringPiSetupGpio() == -1)
return 1;
pinMode(LedPin, OUTPUT);
softPwmCreate(LedPin, 0, 128);
int i;
while(1){
for(i=0;i<129;i+=8){
softPwmWrite(LedPin, i);
delay(100);
}
for(i=128;i>-1;i-=8){
softPwmWrite(LedPin,i);
delay(100);
}
}
return 0;
}
No preview for this file type
#include <wiringPi.h> // GPIO Access Library 헤더파일 선언
#include <softPwm.h> // Software PWM library 헤더파일 선언
// Motor 핀 설정
#include <wiringPi.h>
#include <softPwm.h>
#define MOTOR_MT_N_PIN 17
#define MOTOR_MT_P_PIN 4
// Motor 회전 방향 정의
#define LEFT_ROTATE 1
#define RIGHT_ROTATE 2
// Motor 정지 함수
void MotorStop() {
softPwmWrite(MOTOR_MT_N_PIN, 0);
softPwmWrite(MOTOR_MT_P_PIN, 0);
softPwmWrite(MOTOR_MT_N_PIN, 0);
softPwmWrite(MOTOR_MT_P_PIN, 0);
}
// Motor 회전 함수
void MotorControl(int rotate) {
if (rotate == LEFT_ROTATE) {
digitalWrite(MOTOR_MT_P_PIN, LOW);
softPwmWrite(MOTOR_MT_N_PIN, 50);
if (rotate == LEFT_ROTATE) {
digitalWrite(MOTOR_MT_P_PIN, LOW);
softPwmWrite(MOTOR_MT_N_PIN, 50);
}
else if (rotate == RIGHT_ROTATE) {
digitalWrite(MOTOR_MT_N_PIN, LOW);
softPwmWrite(MOTOR_MT_P_PIN, 50);
}
}
else if (rotate == RIGHT_ROTATE) {
digitalWrite(MOTOR_MT_N_PIN, LOW);
softPwmWrite(MOTOR_MT_P_PIN, 50);
}
}
int main(void)
{
if (wiringPiSetupGpio() == -1)
return 1;
// Motor 핀 출력으로 설정
pinMode(MOTOR_MT_N_PIN, OUTPUT);
pinMode(MOTOR_MT_P_PIN, OUTPUT);
// Motor 핀 PWM 제어 핀으로 설정
// 주기: 100ms
softPwmCreate(MOTOR_MT_N_PIN, 0, 100);
softPwmCreate(MOTOR_MT_P_PIN, 0, 100);
while (1)
{
MotorControl(LEFT_ROTATE); // Motor 왼쪽으로 회전
delay(2000);
MotorStop(); // Motor 정지
delay(1000);
MotorControl(RIGHT_ROTATE); // Motor 오른쪽으로 회전
delay(2000);
MotorStop(); // Motor 정지
delay(1000);
}
return 0;
int main(void){
if (wiringPiSetupGpio() == -1)
return 1;
pinMode(MOTOR_MT_N_PIN, OUTPUT);
pinMode(MOTOR_MT_P_PIN, OUTPUT);
softPwmCreate(MOTOR_MT_N_PIN, 0, 100);
softPwmCreate(MOTOR_MT_P_PIN, 0, 100);
while (1){
MotorControl(LEFT_ROTATE);
delay(2000);
MotorStop();
delay(1000);
MotorControl(RIGHT_ROTATE);
delay(2000);
MotorStop();
delay(1000);
}
return 0;
}
......