Uploaded by zawminminhtun

2022short

advertisement
1 TECHNO HALO: Basic Microcontroller Training Course
TECHNO HALO
ENGINEERING TRAINING CENTER
BASIC MICROCONTROLLER TRAINING COURSE
Dec 2016
Prepared By
U Zaw Min Min Htun
M.Sc Eng (IT) BMSTU, Russia
Phone 09444044046
2 TECHNO HALO: Basic Microcontroller Training Course
Table 1: Types for C and C++
Type Name
Bytes
Other Names
Range of Values
int
4
signed
–2,147,483,648 to 2,147,483,647
unsigned int
4
unsigned
0 to 4,294,967,295
__int8
1
char
–128 to 127
unsigned __int8
1
unsigned char
0 to 255
__int16
2
short, short int, signed short
int
–32,768 to 32,767
unsigned __int16
2
unsigned short, unsigned
short int
0 to 65,535
__int32
4
signed, signed int, int
–2,147,483,648 to 2,147,483,647
unsigned __int32
4
unsigned, unsigned int
0 to 4,294,967,295
__int64
8
long long, signed long long
–9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
unsigned __int64
8
unsigned long long
0 to 18,446,744,073,709,551,615
bool
1
none
false or true
char
0 to 255
signed char
1
none
–128 to 127
unsigned char
1
none
0 to 255
short
2
short int, signed short int
–32,768 to 32,767
unsigned short
2
unsigned short int
0 to 65,535
long
4
long int, signed long int
–2,147,483,648 to 2,147,483,647
unsigned long
4
unsigned long int
0 to 4,294,967,295
long long
8
none (= __int64)
–9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
unsigned long
long
8
none (= unsigned __int64)
0 to 18,446,744,073,709,551,615
float
4
none
3.4E +/- 38 (7 digits)
double
8
none
1.7E +/- 308 (15 digits)
wchar_t
2
__wchar_t
0 to 65,535
3 TECHNO HALO: Basic Microcontroller Training Course
4 TECHNO HALO: Basic Microcontroller Training Course
Example 1: Digital Write for PIC
void main() {
TRISC = 0b00000000;
PORTC=0x00;
while (1){
PORTC=0b00000011;
Delay_ms(200);
PORTC=0b00001100;
Delay_ms(200);
PORTC=0b00110000;
Delay_ms(200);
PORTC=0b11000000;
Delay_ms(200);
}
}
MikroC က ိုဖ ငြ ့လ္ ကို က္ ာ ယ ခ င ္Project မ ာ ်ားပ ြင ့ေ္ န ပ ါက
Project မ ွClose Project/Close Project Group
လ ိုပ ္ ာ ်ားပ ါ။ Project အ သ စ က္ ိုအ ျခ ာ ်ား Project မ ရ သွ ည ့္folder အ သ စ ္ တဲ ြင ္create လ ိုပ ္ပ ါ။
5 TECHNO HALO: Basic Microcontroller Training Course
Example 2: Digital Write for Arduino
void setup()
{
DDRD = B11111111;
PORTD=0x00;
}
void loop()
{
PORTD=B11000000;
delay(200);
PORTD=B00110000;
delay(200);
PORTD=B00001100;
delay(200);
PORTD=B00000011;
delay(200);
}
6 TECHNO HALO: Basic Microcontroller Training Course
Example 3: Digital Read/Write for PIC
void main()
{
ANSELH=0;TRISB.B0=1;TRISB.B1=1;
TRISD.B2=0;TRISD.B3=0;
while(1)
{
if(RB0_bit) PORTD.B2=1; else PORTD.B2=0;
if(RB1_bit) PORTD.B3=1; else PORTD.B3=0;
}
}
-------------------OR------------------void main()
{
ANSELH=0;TRISB.F0=1;TRISB.F1=1;
TRISD.F2=0;TRISD.F3=0;
while(1)
{
if(!PORTB.B0) PORTD.F2=0; else PORTD.F2=1;
if(PORTB.F1==1) PORTD.F3=1; else PORTD.F3=0;
}
}
7 TECHNO HALO: Basic Microcontroller Training Course
Example 4: Digital Read/Write for Arduino
void setup()
{
DDRB&=~bit(0); DDRB&=~bit(1);
DDRD|=bit(2); DDRD|=bit(3);
}
void loop()
{
if (PINB&bit(0)) { PORTD|=bit(2); } else { PORTD&=~bit(2);}
if (PINB&bit(1)) { PORTD|=bit(3); } else { PORTD&=~bit(3);}
}
-------------------OR------------------void setup()
{
pinMode(8,INPUT); pinMode(9,INPUT);
pinMode(2,OUTPUT); pinMode(3,OUTPUT);
}
void loop()
{
if (digitalRead(8)) digitalWrite(2,HIGH); else digitalWrite(2,LOW);
if (digitalRead(9)) digitalWrite(3,HIGH); else digitalWrite(3,LOW);
}
8 TECHNO HALO: Basic Microcontroller Training Course
Example 5: Analog Read and Digital Write for PIC
int adc0to1023;
void main() {
TRISA = 0b00000001;//ANSEL=0b00000001;
TRISC = 0b00000000; TRISD = 0b00000000;
while (1){
adc0to1023 = ADC_Read(0);
if (adc0to1023>=0 & adc0to1023<100) {PORTC=0b00000000; PORTD=0b00000000;}
if (adc0to1023>=100 & adc0to1023<200) {PORTC=0b00000001; PORTD=0b00000000;}
if (adc0to1023>=200 & adc0to1023<300) {PORTC=0b00000011; PORTD=0b00000000;}
if (adc0to1023>=300 & adc0to1023<400) {PORTC=0b00000111; PORTD=0b00000000;}
if (adc0to1023>=400 & adc0to1023<500) {PORTC=0b00001111; PORTD=0b00000000;}
if (adc0to1023>=500 & adc0to1023<600) {PORTC=0b00011111; PORTD=0b00000000;}
if (adc0to1023>=600 & adc0to1023<700) {PORTC=0b00111111; PORTD=0b00000000;}
if (adc0to1023>=700 & adc0to1023<800) {PORTC=0b01111111; PORTD=0b00000000;}
if (adc0to1023>=800 & adc0to1023<900) {PORTC=0b11111111; PORTD=0b00000000;}
if (adc0to1023>=900 & adc0to1023<1000) {PORTC=0b11111111; PORTD=0b00000001;}
if (adc0to1023>=1000 & adc0to1023<1024) {PORTC=0b11111111; PORTD=0b00000011;}
}
9 TECHNO HALO: Basic Microcontroller Training Course
Download