Script_XC888_091211

November 11, 2017 | Author: Tân Chip | Category: Analog To Digital Converter, Electronic Engineering, Computer Programming, Electronics, Technology
Share Embed Donate


Short Description

Download Script_XC888_091211...

Description

Prof. Dr. Peter Nauth

Microcontroller XC888 and ATMega128L

Programming and Applications

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

XC 888 Program structure and Port I/0 C-Programming the XC888 requires to include the Register-Headerfile #include and to set the port directions by means of the Portdirection Register Pi_DIR = 0xFF; Pi_DIR = 0x00;

// Port Pi is set to output (Pi = P0 – P7) // Port Pi is set to input

Interrupts The portpins for the IRQ-signal of external interrupts can be selected by the register MODPISEL: Ext. IR

Portpin

Portpin

EX0 EX1 EX2 EX3 EX4 EX5 EX6

P0.5 if MODPISEL.1=0 P2.0 if MODPISEL.2=0 P2.1 if MODPISEL.3=0 P1.1 P3.7 P1.5 P4.2

P1.4 if MODPISEL.1=1 P5.0 if MODPISEL.2=1 P5.1 if MODPISEL.3=1 n.a. n.a. n.a. n.a.

At the end of the interrupt serviceroutine of external interrupt j, the respective interrupt flag must be cleared by setting the j-th bit in the register IRCON0 to 0. E.g., at the end of the ISR of ext. IR0 the necessary instruction is: IRCON0&=~(ubyte)0x01 Serial Interface Data In- and Output via the serial interface RS232, which is available in the simulation and controller mode, requires header files, libraries and functions as follows:

Univ. Appl. Scs. Frankfurt a.M. Seite 2 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

#include #include #include"UART.h" void main(void) { ...

// Variable declarations

UART_vInit(); printf (“Text%d%d”, x,y);

// Print string and variables x & y (decl. necess.!)

....... } Student Exercise: Write a program in C that displays 16 characters from a string of any length. By displaying character 0 .. 15 first, then character 1 .. 16, then character 2 .. 17 an so on, the string seems to move from left to right. Hints:

#include additionally for string operations Don’t forget to copy UART.h and UART.c in your working directory

Structure: Declare one character array containing the string embedded in 16 blanks before and 16 blanks after: char *buffer ={" Merry Christmas and a happy new year "}; Declare another character array for storing 16 characters from the string char buffer2[16]; Get the length of buffer and substract 16 laenge_total=strlen (buffer); laenge=laenge_total-16; In a while loop, perform operations as follows: - k=i - Copy 16 characters from buffer to buffer2 using a for-loop, then print buffer2[j]=buffer[j+i]; //j is loop counter and runs from 0 to 15 printf("%s\r",buffer2); - i=k - To get the next 16 characters, increment i - If i==laenge, then set i=0 in order to begin with the 1st blank , again.

Univ. Appl. Scs. Frankfurt a.M. Seite 3 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

LCD The LCD is connected with the XC 888 controller as follows: DB RS E RW

P5 P4_6 P4_5 P4_4

The libraries and functions used are: #include #include #include"LCD.h" void main(void) { ...

// Variable declarations

lcd_init(); lcd_clear();

// Init LCD // Clear display

set_cursor(0,1); lcd_print("TI-Labor

// Set Cursor to 1st row, 2nd line // Print string

");

sprintf(text,"%03d",i); lcd_print(text);

// Coversion (declar. int i; char text[3]; necess.!) // Print variable

... } Student Exercise: Write a program in C, which calculates the time passed since program start by means of timer 0 in mode 2 and displays it on the LCD (or serial window in simulation mode) as h:min:sec.

Univ. Appl. Scs. Frankfurt a.M. Seite 4 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

AD converter (ADC) AD converters are used for the acquisition of analog signals, e.g. from sensors. The XC888 has 8 AD-channels connected with the Pins of P2. The acquisition range is 0V to 5V, the digitization width can be set between 8 bit and 10 bit. Typically a digitization width of 10 bit is used. The digitized value Udig can be calculated from an analog signal Uana according to (R= acquisition range, W = digitization width): Udig = Uana (2**W)/R Applied to an acquisition range of R = 0 .. 5V and a digitization width of W = 10 bit, we get: Udig = 204,5 Uana/V More than 20 registers are available for controlling the ADC [3]. The most important are: ADC_GLOBCTR: Global control register Port 2 is set to analog input, if Bit 7 is set to 1 ADC_CHCTRi: Channel control registers, one per channel i (i = 0 .. 7) The lower two bits define, to which of the 4 (j = 0 .. 3) result register RESRj the result of channel i is written ADC_CRPR1: Conversion request pending register 1 If bit i is set, conversion for channel i starts ADC_RESRjL: Lower result register j If Bit 4 (VF-Bit) has been set after conversion, the result is valid Bit 6 and Bit 7 contain the 2 lower bits (Bit 0 and Bit 1) of the conversion result ADC_RESRjH: Higher result register j Contains the 8 higher bits (Bit 3 .. Bit 9) of the conversion result As for programming, functions provide the proper setting of these registers. First the ADCs to be used must be activated by selecting the channel number ChN by means of the function: ADC_vStartParReqChNum(ChN); The ChN is the hex-number resulting from the Byte in which the bits of the corresponding ADC Numbers (0 .. 7) have been set. E.g., ADC 3 is activated by Univ. Appl. Scs. Frankfurt a.M. Seite 5 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

ADC_vStartParReqChNum(0x08); and both, ADC 6 and ADC 7 are activated by ADC_vStartParReqChNum(0xC0); The result of the ADC with the lower number is the return value of ADC_uwGetResultData0(); The result of the ADC with the higher number is the return value of ADC_uwGetResultData1(); Student Exercise: Write a program which digitizes 2 analog signals through ADC 6 and ADC 7 and displays them on the LCD (or serial window in simulation mode).

Pulse-Width Modulation For the control of servo motors, PWM (pulse width modulation) signals are used:

Impulsbreite: 0,550 ms

Univ. Appl. Scs. Frankfurt a.M. Seite 6 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

The PWM uses a timer 12 and is activated by CC6_vStartTmr_CC6_TIMER_12(); CC6_vEnableShadowTransfer_CC6_TIMER_12(); The PWM generation for a certain pulse-width ratio is started by means of CC6_vLoadChannelShadowRegister_CC6_CHANNEL_0(pwv); pwv is the value loaded into the capture and compare register CC60SRH+CC60SRL and calculated as follows pwv = pulseperiode/(2T12CLK * CLK) – pulsewidth /(2T12CLK * CLK) For the CLK is 41,6 ns and we choose T12CLK as 3, we get pwv = pulseperiode/(8 * 41,6 ns) – pulsewidth /(8 * 41,6 ns) Applying this to a pulse-width ratio of 2,75% (pulseperiode of 20 ms and a pulse width of 0,55 ms) in order to set a motor position of -90°, we get: pwv = 60096 – 0,55ms/(23 * 41,6ns) = 60096 – 1653 = 58443 = 0xE44B Hence, the motor position of -90° is set by calling the function CC6_vLoadChannelShadowRegister_CC6_CHANNEL_0(0xE44B);

Univ. Appl. Scs. Frankfurt a.M. Seite 7 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

Student Exercise: Write a program which generates a pulse width modulated signal with a pulse width of 0,1 ms and a pulse periode of 20 ms. In order to read the motor position, a 50 µs impulse must be sent to the servo motor. The signal length of the motor response is correlated with the motors’ position (550µs = -95°, 2450µs = 95°).

Univ. Appl. Scs. Frankfurt a.M. Seite 8 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

Atmel ATMega 128L The Atmel ATMega 128L can control up to 24 servo motors via PWM and provides 8 ADC channels. One application example is the control of the humanoid robot Robonova 1 [4]. By means of appropriate sensors and intelligent algorithms it can operate autonomously and is a good example for as embedded intelligent system. The programming language is C or Robobasic [5].

Univ. Appl. Scs. Frankfurt a.M. Seite 9 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

Legs and arms of the humanoid Roboter ROBONOVA are driven by 2 x 5 Servomotors and 2 x 3 Servomotoren, respectively:

:

The motors are organized in groups as follows: Motor Group A – Left leg B – Left arm C – Right arm D – Right Leg

Motor 0

Motor 1

Motor number Motor 2 Motor 3

Motor 4

Motor 5

Upper leg

hip

n.c.*

Motor 9

Motor 10

Motor 11

Foot

Lower leg

Knee

Motor 6

Motor 7

Motor 8

Shoulder

Upper arm

Lower arm

n.c.*

n.c.*

Head

Motor 12

Motor 13

Motor 14

Motor 15

Motor 16

Motor 17

Shoulder

Upper arm

Lower arm

n.c.*

n.c.*

n.c.*

Motor 18

Motor 19

Motor 20

Motor 21

Motor 22

Motor 23

Upper leg

hip

n.c.*

Foot

Lower leg

Knee

* n.c. = no connection

Standard connection for proximity sensors and LCD: GP2D12 (Far range sensor): AD 1 GP2D120 (Near range sensor): AD 2 LCD: DI 5

Univ. Appl. Scs. Frankfurt a.M. Seite 10 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

Proximity sensors:

Anschlussbelegung

Far range

GP2D12

Near range GP2D120

Range: d = 12 – 80 cm d = (6787cm / (UDig -3)) – 4 cm

if a 10 Bit ADC is used

Range: d = 3 – 40 cm d = (2914 cm / (UDig +5)) -1 cm

if a 10 Bit ADC is used

Sensor function (Far range): Mathematical model of a general 1/d function: UAnalog = (a(d+b)) + c) By regression analysis or 3 values from the sensor function a, b, c are calculated: d = 12 cm -> UAnalog = 2,0863 V d = 20 cm -> UAnalog = 1,3958 V d = 40 cm -> UAnalog = 0,7682 V -> a = 33,14 Vcm b = 4 cm c = 0,015 V UAnalog = (33,14 Vcm/(d+4 cm)) + 0,015 V) UDig = UAnalog ((2**DigWidth)/5V) and DigWidth = 10 Bit -> UDig = (6787cm/(d+4cm)) + 3) d = (6787cm / (UDig -3)) – 4 cm

Univ. Appl. Scs. Frankfurt a.M. Seite 11 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

Measured output signals

Far Range Sensor: UAnalog = (33,14 Vcm/ (d + 4 cm)) + 0,015 V)

Near Range Sensor. UAnalog = (14,23 Vcm/ (d + 1cm)) - 0,0244V)

Univ. Appl. Scs. Frankfurt a.M. Seite 12 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

Program Example 1 Initialization, Main, Subroutines, Jump Instructions und Standard Pose

Univ. Appl. Scs. Frankfurt a.M. Seite 13 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

Program Example 2 Proximity Sensors

Univ. Appl. Scs. Frankfurt a.M. Seite 14 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

Program Example 3 Feedbacksignals of Servo Motors

Univ. Appl. Scs. Frankfurt a.M. Seite 15 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

Program Example 4 Remote Control

Univ. Appl. Scs. Frankfurt a.M. Seite 16 of 17

04.11.2011

MC XC888 and ATMega128L

Prof. Dr. Peter Nauth

References [1] Peter Nauth, Sript_MCTegl_131010 [2] XC800 Users’s manual, Infineon [3] XC866/888CLM Data Sheet, Infineon [4] Robonova-I English user manual [5] Robobasic English Command Instruction Manual [6] www.infineon.com [7] www.keil.com

Univ. Appl. Scs. Frankfurt a.M. Seite 17 of 17

04.11.2011

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF