ภาษาซีส าหรับ - t U...ว ตถ ประสงค เพ อให ม...

Post on 24-Feb-2020

6 views 0 download

Transcript of ภาษาซีส าหรับ - t U...ว ตถ ประสงค เพ อให ม...

1

ภาษาซส าหรบ AVR Microcontroller

รศ.ณรงค บวบทองภาควชาวศวกรรมไฟฟาและคอมพวเตอร

คณะวศวกรรมศาสตร มหาวทยาลยธรรมศาสตร

วตถประสงค

เพอใหมความเขาใจเกยวกบภาษา ซ

เพอใหเขยนโปรแกรมภาษา ซ ส าหรบไมโครคอนโทรลเลอรได

2

ความรเบองตนของภาษา C

พ.ศ.2515 เดนนส รตช (Dennis Ritchie) ไดพฒนาภาษา C ขน พ.ศ. 2531 ไดรปแบบมาตราฐานของภาษา C ทเรยกวา ANSI-C

3

ตวแปลภาษา C ส าหรบ AVR Microcontroller

WinAVRTM (pronounced "whenever") is a suite of

executable, open source software

development tools for the Atmel AVR series

of RISC microprocessors hosted on the Windows platform. It includes the GNU GCC

compiler for C and C++.

4

รปแบบโครงสรางภาษา C

5

1. Header File

2. ประกาศคาคงทและตวแปรชนดโกลบอล

3. ฟงกชน

4. ฟงกชน main

การประกาศ Header File หรอพรโพรเซสเซอรไดเรคทฟ(Preprocessor Directive)

6

ในสวนนจะเปนการแจงใหคอมโพลเลอรทราบถงไฟล ตางๆ ทเกบ ชดค าสงทซพยสามารถจะเรยกใช

ได เปนชดค าสงทไดเตรยมใหซพยสามารถเรยกใช

ไดภายในโปรแกรม โดยกอนทจะมการคอมไพลโปรแกรมใหเปนภาษาเครองนนคอมโพลเลอรจะแปลค าสงในสวนนกอน เชน

// ชดค าสงมาตรฐานอนพท เอาทพท#include <stdio.h>

//ชดค าสงอนพทเอาทพทพอรทของ AVR#include <avr/io.h>

สวน Preprocessor

#include <AT89X051.h>

เปนไฟล Preprocessor ของตระกล AT89C1051 และ AT89C2051

#include <avr/io.h>

ชดค าสงอนพทเอาทพทพอรทของ AVR

7

คอมโพลเลอร ใชสวนนเปนบอกใหรวาในไมโครคอนโทรลเลอรเบอรนนมอะไรบาง เชนรจสเตอรและบตควบคมตางๆ ของคอนโทรลเลอรนน การประกาศใชเครองหมายชารป (#) และไดเรกทฟ include รวมกบชอไฟลทบอกรายละเอยดของคอนโทรลเลอร

สวนไลบราร (Library)

#include <stdio.h>

ประกอบดวยฟงกชนทใชตดตอกบพอรทอนกรม ตวอยางฟงกชนไดแก printf _getkey getchar gets putchar puts scanf sprintf sscanf ungetchar vprintf และ vsprintf

#include <math.h>

ประกอบดวยฟงกชนทใชค านวณทางคณตศาสตร

#include <string.h>

ประกอบดวยฟงกชนทใชจดการเกยวกบสตรง (String) และบฟเฟอร (Buffer)

#include < intrins.h >

ประกอบดวยฟงกชนเกยวกบการหมนบต และการตรวจสอบสถานะของเลขแบบ Floating-point

8

ไลบรารเปนไฟลทมฟงกชนบรรจอยภายใน ไฟลนมนามสกลเปน .h มาพรอมกบตวแปล สามารถตรวจสอบไดจากไฟล Help

การประกาศใชไดเรกทฟ #include แลวตอดวยชอไฟล

สวนประกาศคาคงทและตวแปรชนดโกลบอล

9

การก าหนดคาคงท และชอแทน (Definitions)เปนการก าหนดชอแทนใหกบคาคงท หรอชดค าสง ตางๆ#define max_time 10 #define cpu_clk 8000000

การประกาศคาตวแปร (Declarations)เปนการประกาศใชงานตวแปรดวยการก าหนดตวแปร และชนดขอมลของตงวแปร เชนunsigned char i,j; // ประกาศคาตวแปร i และ j เปน

// ชนด unsigned char int max = 10; //ประกาศคาตวแปร max เปน

// ชนด int พรอมทงก าหนดคาเรมตน

สวนการใชงานรวม (Global Declarations)

ตวแปรแบบใชงานรวมหรอ

ตวแปรแบบโกลบอล

หมายถงตวแปรทสามารถเรยกใชไดทกทภายในโปรแกรม

การประกาศใหตวแปรเปนแบบโกลบอล ใหประกาศใวนอกขอบเขตของสวนโปรแกรมใดๆ

ตวแปรแบบโลคล (Local)

ตวแปรใชใดเฉพาะภายในสวนของโปรแกรมใดโปรแกรมหนง

การประกาศใหประกาศใวภายในโปรแกรมทตองการใชงาน

10

/*----------------------------------------------*/

#include <avr/io.h>

#include <stdio.h>

unsigned int i;

Static void delay_1s(void)

{

}

/*------MAIN C function -------------------*/

void main (void)

{

char c;

USART_Init(51);

while (1)

{

delay_1s();

fprintf (&uart_str"\nYou program\n");

}

}

สวนคาคงทของโปรแกรมการประกาศคาคงทในภาษา C ใชไดเรกทฟ #defined แลวตามดวยคาคงทดงน

#define setValue 99 ก าหนดคา setValue ใหเปน 99

หรอใชแทนการเขยนค าสงเพอใหสอความหมายไดชดเจน

#define LED_ON PORTD |= 0b00000001

#define LED_OFF PORTD &= 0b11111110

11

สวนของการสรางฟงกชน

12

เปนสวนทผใชงานสามารถสรางฟงกชน หรอโปรแกรมยอยเพอการใชงานตางๆ ไดเองตามทตองการ โดยปกต จะเขยนไวกอนฟงกชน main ( ) เพอใหฟงกชน main ( ) สามารถเรยกใชงานฟงกชนตางๆ ทเราสรางขนมาได

ฟงกชน (Function) โปรแกรมหนงๆ จะมกฟงกชนกได

ตองมฟงกชนหลกอยหนงฟงกชน ใชเปนฟงกชนส าหรบเรมการท างานของโปรแกรม เรยกฟงกชนนวา main

สวนของค าสงของฟงกชนจะอยภายในวงเลบปกกา { }และสามารถก าหนดใหมการสงผานตวแปรได หรออารกวเมนตของฟงกชน (Function Arguments) ถาไมตองการใหสงตวแปรเขาจะใชค าวา void ใวภายในวงเลบและถาไมมการสงคาคน จะใสค าวา void ใวหนาชอฟงกชน ตามมาตราฐาน ANSI C มไดสงสด 31 ตว ตวอยางเชน

void func_abcd(void)

{

…………………………..

}

void delay_ms(int t) //ฟงกชนนม อารกวเมนตหรอพารามเตอรสงใหฟงกชน 1 ตว

{

}

13

ฟงกชน main ( )

14

ส าหรบฟงกชนนถอเปนฟงกชนหลกของโปรแกรมภาษา C ทจ าเปนตองมเสมอและจะมไดเพยงฟงกชนเดยวเทานน หนาทส าคญของฟงกชนนคอ เปนจดเรมตนการท างานของโปรแกรมนนเอง ดงนนในการเขยนโปรแกรมจงเรมตนทฟงกชนนเสมอ

สวนหมายเหต (Comment)

เปนสวนทใหผเขยนโปรแกรมใชอธบายความหมายของโปรแกรมหรอค าสงทเขยน เพอใหงายตอการท าความเชาใจ สวนนตองอยในเครองหมาย /* …… */ หรออยหลงเครองหมาย //

/*----------------------------------------------------------------------------------

Set serial port for 9600 baud at 11.0592 MHz. Note that we use Timer 1

for the baud rate generator.

---------------------------------------------------------------------------------*/

/* ------------------------- Main Function -------------------------------*/

// PS2 I/P Program

15

ขอก าหนดอนๆ

ค าสงสวนใหญตองเขยนดวยอกษรตวพมพเลก ตวแปรสามารถตงชอเปนตวพมพเลกหรอใหญกไดแตตองเรยกใชให

ถกตามตวพมพ และตองไมตรงกบค าสงวนของตวคอมไพเลอรทใช ทกค าสงตองมเครองหมาย ; แสดงการจบค าสง และสามารถเขยน 1

ค าสงใน 1 บรรทดหรอจะเขยนตอกนยาวกได แตกจะท าใหแกไขโปรแกรมไดยาก

ค าสงใดๆในโปรแกรมสามารถมเลเบลไดถาตองการ และตองมเครองหมาย : ตามทายดวย

16

ขอระบอนๆ

นพจน (Expressions)

คอการกระท าระหวางตวด าเนนการ (Operators) กบตวกระท า (Operands) เพอใหเกดคาใดคาหนง

i = j+10;

สเตทเมนต (Statements)

หมายถงค าสงเพอใหเกดการท าการตามความตองการ

while(i<10)

{

a = i+3;

i = i+1

}

ตวอยาง/*----------------------------------------------*/

#include <avr/io.h>

#include <stdio.h>

unsigned int i;

Static void delay_1s(void)

{

}

/*------MAIN C function -------------------*/

void main (void)

{

char c;

USART_Init(51);

while (1)

{

delay_1s();

fprintf (&uart_str"\nYou program\n");

}

}

18

การประกาศ Preprocessor ชดค าสงอนพทเอาทพทพอรทของ AVR

การประกาศไลบราร stdio ส าหรบฟงกชนการใชงานพอรทอนกรม และ ไลบราร ctype

เรยกใชฟงกชน delay_1s

การเขยนฟงกชน

สวนหลกของโปรแกรม ไมมการสงขอมลเขาและไมมขอมลออก

ฟงกชน printf เปนการสงขอมลออกทางพอรทอนกรม

คาคงทและตวแปรชนดของตวแปร

19

ชนด (Type) ชอแทน ชวงของคาทเกบ(Range)

Bits Bytes

char 8 1 -128 to +127

signed char int8_t 8 1 -128 to +127

unsigned char uint8_t 8 1 0 to 255

int 16 2 -32768 to +32767

signed int int16_t 16 2 -32768 to +32767

unsigned int uint16_t 16 2 0 to 65535

long 32 4 -2147483648 to 2147483647

signed long int int32_t 32 4 -2147483648 to 2147483647

unsigned long int uint32_t 32 4 0 to 4294967296

signed long long int int64_t 64 8

unsigned long long int uint64_t 64 8 0 to 2^64

float 64 8 3.4x10-38 to 3.4x1038

ขนาดความกวาง

ตวด าเนนการตวด าเนนการทางคณตศาสตร

เครองหมาย ความหมาย

+ บวก

- ลบ

* คณ

/ หาร

% หารแบบใชเศษของการหาร(Mod)

20

ตวด าเนนการ ตวด าเนนการทใชในการแปลงคา

เครองหมาย ความหมาย= น าคาทางขวามาใหทางซาย++ เพมคาขน 1-- ลดคาลง 1+= เพมคาเทากบคาทางขวา-= ลดคาเทากบคาทางขวา*= น าตวเองคณกบคาทางขวา/= น าตวเองหารกบคาทางขวา%= น าตวเองหารกบคาทางขวาเอาเศษ&= น าตวเองมาAND กบคาทางขวา|= น าตวเองมา OR กบคาทางขวา^= น าตวเองมา XORกบคาทางขวา<<= น าตวเองมาเลอนบตไปทางซายจ านวนครงเทากบคาทางขวา>>= น าตวเองมาเลอนบตไปทางขวาจ านวนครงเทากบคาทางขวา

21

ตวด าเนนการตวด าเนนการทใชในการเปรยบเทยบและตวด าเนนการทางบต

เครองหมาย ความหมาย== เทากบ!= ไมเทากบ! ตรงกนขาม(NOT)&& และ (AND)>= มากกวาหรอเทากบ<= นอยกวาหรอเทากบ|| หรอ(OR)< นอยกวา> มากกวา

22

เครองหมาย ความหมาย

& AND บต

| OR บต

^ XOR บต

<< Left shift บต

>> Right shift บต

~ One' Complement (Inverse)

อะเรยและพอยนเตอร (Array and Pointer)

ตวแปรอะเรย

ตวแปรอะเรย คอกลมของตวแปร เชนกลมของตวแปร char กลมของตวแปร int สมาชกของกลมตวแปรนมจ านวนจ ากด เฉพาะเจาะจง เมอก าหนดจ านวนแลวไมสามารถลดหรอเพมได ดงนนการก าหนดอะเรยส าหรบไมโครคอนโทรลเลอรตองค านงถงหนวยความจ าทมใชงานดวย อะเรยนสามารถก าหนดใหเปนแบบหลายๆมตได ขนอยกบวาตวแปรภาษายอมใหมไดเทาไร

รปแบบการก าหนดอะเรย

แบบ 1 มต ชนดของตวแปร ชอตวแปร[n]

แบบ 2 มต ชนดของตวแปร ชอตวแปร[n][m]

โดย n และ m หมายถงจ านวนสมาชก

23

ตวอยางตวแปรและการก าหนดคาใหกบตวแปร

x เปนตวแปร y เปนตวแปร 1 มต z เปนตวแปร 2 มต

24

void main( void ){

unsigned char x, y[3], z[2][3];x = 4;y[0] = 1;y[1] = 2;y[2] = 3;

z[0][0] = 10;z[0][1] = 11;z[0][2] = 12;z[1][0] = 13;z[1][1] = 14;z[1][2] = 15;

}

ตวแปรแบบพอยเตอร (Pointer)

ตวแปรพอยเตอรหรอตวแปรตวช เปนตวแปรทมความส าคญมากในภาษาซ เพราะวาใหความยดหยนในการใชงานไดด คลายกบภาษาแอสเซมบล ตวแปรแบบพอยเตอรมหนาทเกบต าแหนงของตวแปรอนๆ วาอยทใดในหนวยความจ า

รปแบบการก าหนดตวแปรพอยเตอร

ชนดของตวแปร *ชอตวแปร

25

ตวอยางการใชตวแปรแบบพอยเตอร

เปนการก าหนดพอยเตอรข น 2 ตว เพอใช

ชต าแหนงของหนวยความจ าขอมลภายใน

ต าแหนงท 40H

และต าแหนงท 50H

และระบตวแปร a เปนแบบ Unsigned char

การอานขอมลจากหนวยความจ ามาใว ท a กจะอานทละ 8 บต

เทานน แตถาระบ a เปนตวแปรแบบอนเชน char กานอานกจะท าทละ 16 บต ดงนนการก าหนดชนดตวแปรนตองระมดระวงใหด

26

void main( void )

{

unsigned char *ptr1;

unsigned char *ptr2;

unsigned char a;

ptr1 = 0x40;

ptr2 = 0x50;

a = *ptr1;

a = a+5;

*ptr2 = a;

}

ตวอยางการใชตวแปรแบบพอยเตอร 2

27

unsigned char c;

#include <avr/io.h>

int main(void)

{

unsigned char *ptr1;

unsigned char *ptr2;

unsigned char a,c,i;

unsigned char mydata[10];

DDRD = 0x00;

PORTD = 0XFF;

DDRB = 0XFF;

c = PIND;

// = start address of mydata

ptr1 = &mydata;

// = end address of mydata + 1

ptr2 = &mydata+1;

for(i=1;i<11;i++)

{

mydata[i-1] = i;

}

*ptr1 = c;

a = *ptr1;

a = a+5;

*ptr2 = a;

c = *ptr2;

PORTB = c;

}

ค าสงควบคมตางๆในภาษา C

If

if-else

Switch

For

While

do-while

GOTO

28

ค าสง

break หยดการท างานของลป และออกจากลป

continue;ตรงขามกบค าสง break การท างานของค าสง continune จะบงคบให โปรแกรมกระโดดขามไปท างานในรอบตอไปเลยโดยไมตองท าค าสงทอยถดไป

for (i=0;i<10;i++) {

if(i==5) {

continune; } printf("%d\n\r",i);

}

30

ตวอยางโปรแกรมไฟกระพรบท พอรท D บต 0#define F_CPU 1000000UL /* 1 MHz CPU clock */#include <stdio.h>#include <util/delay.h>#include <avr/io.h>#define LED_ON PORTD |= 0b00000001 #define LED_OFF PORTD &= 0b11111110 int main(void){

DDRD = 1; while(1)

{LED_ON; _delay_ms(500);LED_OFF; _delay_ms(500);

}}

Intel HEX file format

Intel HEX file format (*.HEX) เปนไฟลรหส ASCII ซงสามารถใช โปรแกรม EDITOR ทวไปเรยกขนมาดได

:BC AAAA TT HHH...HHCC : เครองหมายโคลอน ใชเปนตวเรมตนขอมล BC จ านวนไบทของขอมลในเรคคอรด มคาเปนเลขฐาน 16 (HEX) (เปน 00 ถาเปนเรคคอรดสนสดไฟล)

AAAA เปนแอดเดรสขอมลไบทแรกในเรคคอรด TT แสดงชนดของเรคคอรด

= 00 ถาเปนเรคคอรดขอมล = 01 ถาเปนเรคคอรดสนสดไฟล

HH ขอมล 1 ไบท จ านวนตามทระบไว ใน BC CC คาของ CHECKSUM ซงเปนคา two's complement ของผลบวก ของขอมลทกไบท ในเรคคอรด

ตวอยางไฟลเฮกซ

:10 0000 00 0C 94 2A 00 0C 94 3F 00 0C 94 3F 00 0C 94 3F 00 89

:100010000C943F000C943F000C943F000C943F0064

:100020000C943F000C943F000C943F000C943F0054

:00000001FF

33

โปรแกรมท 7.2 โปรแกรมควบคมไฟวง#include < at89x051.h > /* Include 89C2051 header file */

#include <intrins.h> /*Intrinsic functions for C51. */

void wait (void)

{ ; /* wait function */

}

void main( void )

{ unsigned int i;

unsigned char a;

unsigned char b;

a = 0x01;

while(1)

{

P1 = a;

b = _crol_(a,1);

a = b;

for ( i = 0; i < 10000; i++ )

{

wait(); /* delay for 97 ms */

}

}

}

จบ

34

สไลดถดจากนไปเปนตวอยางเอาใวศกษา เกยวกบเรอง pointer

access hardware

35

36

C Programming - Review

Types

Functions

Libraries

Pointers

Run-time memory allocation – malloc

37

C Programming - Review

Types

• char, int, float, double

• short, long

• signed, unsigned

• const, volatile

• static, auto, extern

38

C Programming - Review

Functions• Prototype/declaration

• double function( int input_variable );

• Definition• double function( int input_variable )

• { double temp;

• temp = sqrt( (double )input_variable);

• return( temp );

• }

39

C Programming - Review

Libraries#include <stdio.h>

#include <math.h>

#include “my_functions.h”

main()

{ int var=0;

double var2;

var2 = function( var );

}

#include “my_functions.c”

40

C Programming - Review

Pointers• Variable containing addresses

int var, array[4], *ptr;

ptr = &var;

*ptr = 5; /* equiv. to var = 5; */

ptr = &array[1];

var = (*ptr); /* equiv. to var = array[1]; */

• *ptr on left side of argument, data is stored into memory at that address

• *ptr on right side of argument, data is retrieved from memory at that location

41

C Programming - Review

Run-time memory allocation – malloc

#include <stdio.h>

#include <stdlib.h>

main()

{ int *ptr;

ptr = (int *)malloc( sizeof( int ) );

free( ptr );}

42

Accessing Hardware from within C

Although C is a high-level language, itstill allows the programmer to remain intouch with the hardware of a machine.

The microprocessor(s) of a computersystem are capable of addressing anumber of memory locations - theaddress space.

43

Accessing Hardware from within C

A system will have a given amount of RAM, whereprograms and data (e.g. variables) are temporarilystored by programs. The address returned bypointers and function names is an address in theaddress space.

The hardware devices of a system are alsoaccessible in the address space of themicroprocessor. Each device, such as an A/D chip,serial controller etc, will have a unique address orrange of addresses usually taken up by its registers.

44

Accessing Hardware from within C

Any hardware device (or memory location) inthe address space can be accessed via itsaddress and the indirection operator, *.

The address of a device can be stored in apointer; the type of pointer depends on thedevice being accessed. Often registers areone byte (8 bits) wide and are bestrepresented by pointers to type char,specifically unsigned char.

45

Accessing Hardware from within C

It is good practice to create a type to use with

the registers:

• typedef unsigned char BYTE;

• BYTE *control_reg;

The address of the device has to be assigned

to the pointer. Normally the address contained

in a pointer is supplied by the operating system via the address operator, & or malloc, etc.

46

Accessing Hardware from within C

The address of a hardware device will not be known by the compiler so it must be supplied by the programmer. The onus is then on the programmer to ensure that the address is valid.

Addresses can be assigned directly, usually in hexadecimal:• control_reg = 0x1000;

• /* supply address 1000 in hex */

Such a statement may attract a warning from the compiler. An assignment of this nature should involve a cast as a means of indicating that the programmer knows what s/he is doing:• control_reg = (BYTE *)0x1000;

• /* address is cast */

47

Accessing Hardware from within C

Reading and writing bytes to this location can now be achievedby using the * operator.

If a register wider than 8 bits is to be accessed, this can doneby using a pointer of different type.

Example• typedef unsigned int WORD;• WORD *data_register = (WORD *)0x3000;• *data_register = 0x55aa;

Will write:• 0x55 to address 0x3000 and

• 0xaa to address 0x3001, assuming that the pointer contains the least significant address

of an object.

48

Accessing Hardware from within C

The volatile type qualifier should be used

for variables that can change outside the

scope of the function. This is relevant for

variables which change as a result of hardware

interrupts or input/output ports.

Example: #define PORTA *(unsigned

char volatile *)(0x0000)

49

ทมทมเขาสอบทไรใชลอกเพอนครขเตอนจะไลออกถาจบไดสอบปลายปทมจงไมลอกใครท าไมไดกระดาษเปลาเอาสงคน

พวกเพอนเพอนทใหทมไดลอกสอบเสรจบอกกบทมวาอยาขมขน

ขนาดกดหนงสอมาทงคนกยงยนกระดาษเปลาเอาสงคร

ทมโมโหตดพอแลวตอวากอตสาหไมลอกมงตามค าขมงดนสงกระดาษเปลาเอาเหมอนกเดยวคณครเขารหรอกวาลอกกน