microprocessor's People
microprocessor's Issues
multi switch code
#define F_CPU 16000000L
#include <avr/io.h>
#include <util/delay.h>
#define OCR_MAX 255
unsigned char read_switch(unsigned char sw_id);
void control_motor(float vel_ratio, int dir);
// volatile unsigned char pin_bef = 0xff;
int main(void){
DDRC = 0x00;
DDRB = 0xFF;
DDRE = 0xFF;
PORTB = 0x00;
TCCR0 |= (1<<WGM01) | (1<< WGM00);
TCCR0 |= (1<<CS02);
TCCR0 |= (1<<COM01);
OCR0 = 0;
int speed = 0;
while(1){
if(read_switch(0))
speed += 1;
else if(read_switch(1))
speed -= 1;
else if(read_switch(2))
speed = 0;
if(speed > 0)
control_motor(speed*0.2f, 1);
else if(speed < 0)
control_motor(speed*0.2f, -1);
else if(speed == 0)
control_motor(speed*0.2f, 0);
if(speed==0)
PORTE = 0x01;
else if(speed==1)
PORTE = 0x02;
else if(speed==2)
PORTE = 0x04;
else if(speed==3)
PORTE = 0x08;
else if(speed==-1)
PORTE = 0x10;
else if(speed==-2)
PORTE = 0x20;
else if(speed==-3)
PORTE = 0x40;
else
PORTE = 0x00;
_delay_ms(50);
}
}
unsigned char read_switch(unsigned char sw_id){
static unsigned char pin_bef = 0xff;
unsigned char sw_bef = pin_bef & (1 << sw_id);
unsigned char sw_cur = PINC & (1 << sw_id);
if(sw_bef > 0 && sw_cur == 0)
{
_delay_ms(10);
sw_cur = PINC & (1 << sw_id);
pin_bef = (pin_bef & ~(1 << sw_id)) | sw_cur;
if(sw_cur == 0)
return 1;
}
pin_bef = (pin_bef & ~(1 << sw_id)) | sw_cur;
return 0;
}
void control_motor(float vel_ratio, int dir)
{
OCR0 = (int)(OCR_MAX * vel_ratio);
if(dir > 0){
PORTB |= 0x01;
PORTB &= ~0x02;
}
else if(dir < 0){
PORTB &= ~0x01;
PORTB |= 0x02;
}
else {
PORTB &= ~0x01;
PORTB &= ~0x02;
}
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.