Vous êtes sur la page 1sur 50

TP N°1 : Prise en main du Kit Workstation V7

TP N°1 : Prise en main du Kit Workstation V7

1. Objectif
Comprendre et mettre en œuvre la programmation en langage C des Microntroleurs de
Microchip en utilisant le Kit Workstation V7.

Kit Workstation V7

2. Description matérielle et installation des outils


2.1 Description matérielle
 Kit Workstation V7


Carte Workstation V7


 

Par M. JAROU 1|Page


TP N°1 : Prise en main du Kit Workstation V7

Bus d’extension ou Micro Bus


Familles de Microcontroleurs de Microchip

Caractéristiques des Microcontroleurs de Microchip (Face avant)

Par M. JAROU 2|Page


TP N°1 : Prise en main du Kit Workstation V7

Pins des Microcontroleurs de Microchip (Face arrière)

Caractéristiques des Microcontroleurs de Microchip (Face avant)

Pins des Microcontroleurs de Microchip (Face arrière)


 

Par M. JAROU 3|Page


TP N°1 : Prise en main du Kit Workstation V7

Programmateur intégré ou Mikroprog

Mikroprog

Cartes d’extension ou Click Boards

Par M. JAROU 4|Page


TP N°1 : Prise en main du Kit Workstation V7

2.2 Installation des drivers et des outils


Installation de Package Manager Build.3.5.0 :
Dossier compressé : package_manager_v350.zip

Installation de BSP Libraires :


Dossier compressé : mikromedia_workstation_v7_bsp.zip

Par M. JAROU 5|Page


TP N°1 : Prise en main du Kit Workstation V7


Installation des drivers du Kit Worksattion :
Dossier compressé : mikroProg For PIC Drivers v200.zip

Par M. JAROU 6|Page


TP N°1 : Prise en main du Kit Workstation V7

Installation de MicroC For PIC


En connectant la USB Dongle License, installez MicroC For PIC. On
peut mettre à jour MicroC For PIC en cliquant sur « Help/Check For
Updates »

USB Dongle License

USB Dongle plugged into Laptop USB host connector

Par M. JAROU 7|Page


TP N°1 : Prise en main du Kit Workstation V7

3. Activités pratiques

Environnement de MicroC Pro for PIC

Par M. JAROU 8|Page


TP N°1 : Prise en main du Kit Workstation V7

Par M. JAROU 9|Page


TP N°1 : Prise en main du Kit Workstation V7

Par M. JAROU 10 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Par M. JAROU 11 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Par M. JAROU 12 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

 

Par M. JAROU 13 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Environnement de MicroProg Suite for PIC

Par M. JAROU 14 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Par M. JAROU 15 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Par M. JAROU 16 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Par M. JAROU 17 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Par M. JAROU 18 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Par M. JAROU 19 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Par M. JAROU 20 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Objectifs
Le but de cette activité est d’apprendre ………. A la fin de cette séance, vous devez
être capable ……………….
Schéma et description
Travail pratique
Programme source

Par M. JAROU 21 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

/*
* Project name:
Button (Demonstration of using Button Library)
* Description:
This program demonstrates usage on-board buttons.
On every GROUP4 button press coresponding GROUP3 led is
turned off.
* Test configuration:
MCU: PIC18F87J50

Oscillator: HS-PLL 48.0000 MHz, 8.0000 MHz Crystal


Ext. Modules: None.
SW: mikroC PRO for PIC

* NOTES:
- Turn ON the GROUP3 LEDs
- Select VCC button press level and pull down GROUP4

*/

unsigned short value;

void main() {
WS_Init();
WS_GROUP4_Set_Input(WS_BIT_MASK_ALL);
WS_GROUP3_Set_Output(WS_BIT_MASK_ALL);
while(1) {
value = WS_GROUP4_In();
WS_GROUP3_Out(~value);
Delay_ms(100);

Par M. JAROU 22 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Objectifs
Le but de cette activité est d’apprendre ………. A la fin de cette séance, vous devez
être capable ……………….
Schéma et description
Travail pratique
Programme source

/*
* Project name:
ADC_on LEDs (Display the result of ADC on LEDs)

Par M. JAROU 23 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

* NOTES:
- Turn on GROUP3 and GROUP4 LEDs.
- To simulate analog input, place ADC INPUT jumper in A0
position
and use on-board potentiometer P2.
*/

int value;

void main() {
WS_Init();
WS_GROUP4_Set_Output(WS_BIT_MASK_ALL);
WS_GROUP3_Set_Output(WS_BIT_MASK_ALL);
ADC_Init();
while(1) {
value = ADC_Get_Sample(WS_A0);
WS_GROUP4_Out(value & 0x00FF);
WS_GROUP3_Out((value >> 8) & 0x0003);
Delay_ms(100);
}
}

Objectifs
Le but de cette activité est d’apprendre ………. A la fin de cette séance, vous devez
être capable ……………….
Schéma et description
Travail pratique
Programme source

Par M. JAROU 24 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

/*
* Project name: Buzzer

* Description:
This project is a simple demonstration of how to
use sound library for playing tones on a piezo speaker.
* Test configuration:
MCU: PIC18F87J50
Oscillator: HS-PLL 48.0000 MHz, 8.0000 MHz Crystal
Ext. Modules: None.
SW: mikroC PRO for PIC

* NOTES:
- Turn on piezo buzzer at SW4.8
- Select VCC button press level and pull down at GROUP0.3
to GROUP0.7
- Press one of the buttons at GROUP0 to play sound
*/

void Tone1() {
Sound_Play(659, 250);
// Frequency = 659Hz, duration = 250ms
}

void Tone2() {
Sound_Play(698, 250);
// Frequency = 698Hz, duration = 250ms
}

void Tone3() {
Sound_Play(784, 250);
// Frequency = 784Hz, duration = 250ms

Par M. JAROU 25 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

void Melody() {
// Plays the melody "Yellow house"
Tone1(); Tone2(); Tone3(); Tone3();
Tone1(); Tone2(); Tone3(); Tone3();
Tone1(); Tone2(); Tone3();
Tone1(); Tone2(); Tone3(); Tone3();
Tone1(); Tone2(); Tone3();
Tone3(); Tone3(); Tone2(); Tone2(); Tone1();
}

void ToneA() {
Sound_Play( 880, 50);
}
void ToneC() {
Sound_Play(1046, 50);
}
void ToneE() {
Sound_Play(1318, 50);
}

void Melody2() {
unsigned short i;
for (i = 9; i > 0; i--) {
ToneA(); ToneC(); ToneE();
}
}

void main() {
WS_Init();

Sound_Init(&WS_G4_B3_PORT, WS_G4_B3_PIN);
Sound_Play(880, 1000);
// Play sound at 880Hz for 1 second

WS_GROUP0_Set_Input(0xF8);
Melody2();
while (1) {
if (Button(&WS_G0_B7_PORT, WS_G0_B7_PIN, 1, 1))
// G0.7 plays Tone1
Tone1();
while (WS_G0_B7_IN) ;
// Wait for button to be released

if (Button(&WS_G0_B6_PORT, WS_G0_B6_PIN, 1, 1))


// G0.6 plays Tone2
Tone2();
while (WS_G0_B6_IN) ;
// Wait for button to be released

if (Button(&WS_G0_B5_PORT, WS_G0_B5_PIN, 1, 1))


// G0.5 plays Tone3
Tone3();
while (WS_G0_B5_IN) ;
// Wait for button to be released

if (Button(&WS_G0_B4_PORT, WS_G0_B4_PIN, 1, 1))


// G0.4 plays Melody2
Melody2();

Par M. JAROU 26 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

while (WS_G0_B4_IN) ;
// Wait for button to be released

if (Button(&WS_G0_B3_PORT, WS_G0_B3_PIN, 1, 1))


// G0.3 plays Melody
Melody();
while (WS_G0_B3_IN) ;
// Wait for button to be released
}
}

Objectifs
Le but de cette activité est d’apprendre ………. A la fin de cette séance, vous devez
être capable ……………….
Schéma et description
Travail pratique
Programme source

*
* Project name: Joystick
* Description:
This code demonstrates usage of on-board joystick button.

Par M. JAROU 27 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

* Test configuration:
MCU: PIC18F87J50
Oscillator: HS-PLL 48.0000 MHz, 8.0000 MHz Crystal
Ext. Modules: None.
SW: mikroC PRO for PIC
* NOTES:
- Turn on joystick switches at SW4
*/

#include "Joystick_objects.h"

unsigned short Old_State;

void HW_Init() {
WS_Init();
WS_Joystick_Init();
}

unsigned short WS_Joystick_Read(){


return (WS_JOY_DOWN | WS_JOY_LEFT << 1 | WS_JOY_PUSH << 2 |
WS_JOY_RIGHT << 3 | WS_JOY_UP << 4);
}

void main() {
HW_Init();
DrawMainScreen();

Old_State = 0;

while (1) {
if (Old_State != WS_Joystick_Read()){
Old_state = WS_Joystick_Read();

if (WS_Joystick_Push_Pressed()) {
CircleCENTER.Gradient_End_Color = CL_GREEN;
DrawCircle(&CircleCENTER);
}
else {
CircleCENTER.Gradient_End_Color = 0x8208;
DrawCircle(&CircleCENTER);
}

if (WS_Joystick_Left_Pressed()) {
BoxLEFT.Gradient_End_Color = CL_GREEN;
DrawBox(&BoxLEFT);
}
else {
BoxLEFT.Gradient_End_Color = 0x8208;
DrawBox(&BoxLEFT);
}

if (WS_Joystick_Up_Pressed()) {
BoxUP.Gradient_End_Color = CL_GREEN;
DrawBox(&BoxUP);
}
else {
BoxUP.Gradient_End_Color = 0x8208;
DrawBox(&BoxUP);
}

Par M. JAROU 28 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

if (WS_Joystick_Right_Pressed()) {
BoxRIGHT.Gradient_End_Color = CL_GREEN;
DrawBox(&BoxRIGHT);
}
else {
BoxRIGHT.Gradient_End_Color = 0x8208;
DrawBox(&BoxRIGHT);
}

if (WS_Joystick_Down_Pressed()) {
BoxDOWN.Gradient_End_Color = CL_GREEN;
DrawBox(&BoxDOWN);
}
else {
BoxDOWN.Gradient_End_Color = 0x8208;
DrawBox(&BoxDOWN);
}
Delay_ms(50);
}
}
}

Objectifs
Le but de cette activité est d’apprendre ………. A la fin de cette séance, vous devez
être capable ……………….
Schéma et description
Travail pratique
Programme source

Par M. JAROU 29 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

/*
* Project name: LM35
* Description:
This code demonstrates usage of analog temperature sensor
LM35
MCU reads temperature from the sensor and prints it on
the mikromedia.
* Test configuration:
MCU: PIC18F87J50
Oscillator: HS-PLL 48.0000 MHz, 8.0000 MHz Crystal
Ext. Modules: None.
SW: mikroC PRO for PIC

* NOTES:
- Place LM35 sensor in proper position and place jumper
J4 in G0.0 position.
*/

#include "Resources.h"

/*************************************
TFT module connections
*************************************/

char TFT_DataPort at LATJ;


sbit TFT_RST at LATD3_bit;
sbit TFT_RS at LATE0_bit;
sbit TFT_CS at LATD2_bit;
sbit TFT_RD at LATH1_bit;
sbit TFT_WR at LATH2_bit;

Par M. JAROU 30 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

sbit TFT_BLED at LATC2_bit;

char TFT_DataPort_Direction at TRISJ;


sbit TFT_RST_Direction at TRISD3_bit;
sbit TFT_RS_Direction at TRISE0_bit;
sbit TFT_CS_Direction at TRISD2_bit;
sbit TFT_RD_Direction at TRISH1_bit;
sbit TFT_WR_Direction at TRISH2_bit;
sbit TFT_BLED_Direction at TRISC2_bit;

/*************************************
End of TFT module connections
*************************************/

unsigned temperature = 0, old_temperature = 1;


char s_temp[14];
float f_temp;

void DrawMainScr() {
TFT_Fill_Screen(CL_WHITE);
TFT_Set_Pen(CL_Black, 1);
TFT_Line(20, 220, 300, 220);
TFT_Line(20, 46, 300, 46);
TFT_Set_Font(&HandelGothic_BT21x22_Regular, CL_RED,
FO_HORIZONTAL);
TFT_Write_Text("LM35 TEST", 105, 14);
TFT_Set_Font(&HandelGothic_BT21x22_Regular, CL_BLACK,
FO_HORIZONTAL);
TFT_Write_Text("TEMPERATURE", 85, 85);
TFT_Set_Font(&Verdana12x13_Regular, CL_BLACK,
FO_HORIZONTAL);
TFT_Write_Text("mikroMedia Workstation", 19, 223);
TFT_Write_Text("www.mikroe.com", 200, 223);
TFT_Set_Font(&TFT_defaultFont, CL_BLACK, FO_HORIZONTAL);
}

void Display_Temperature(unsigned temp) {


TFT_Set_Brush(1, CL_WHITE, 0, 0, 0, 0);
TFT_Set_Pen(CL_RED, 1);
TFT_Rectangle(115, 115, 200, 145);

f_temp = (float)(temp * 0.322265);

FloatToStr(f_temp,s_temp);

s_temp[5] = ' ';


s_temp[6] = 'C';
s_temp[7] = 0;

TFT_Set_Font(&HandelGothic_BT21x22_Regular, CL_BLACK,
FO_HORIZONTAL);
TFT_Write_Text(s_temp, 120, 120);
}

void main() {
WS_Init();

TFT_Init(320,240);
DrawMainScr();

Par M. JAROU 31 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

ADC_Init();

// Main loop
do {
// Perform temperature reading
temperature = ADC_Get_Sample(WS_G0_B0_AN);

// Format and display result on TFT


if ((temperature > old_temperature+1) || (temperature <
old_temperature-1)) {
// Add hysteresis to prevent display flickering
Display_Temperature(temperature);
old_temperature = temperature;
}
Delay_ms(100);
} while (1);
}

Par M. JAROU 32 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Objectifs
Le but de cette activité est d’apprendre ………. A la fin de cette séance, vous devez
être capable ……………….
Schéma et description
Travail pratique
Programme source

/*
* Project name: DS1820
* Description:
This code demonstrates one-wire communication with
temperature sensor
DS18x20.
MCU reads temperature from the sensor and prints it on
the mikromedia.
The display format of the temperature is 'xxx.xx°C'. To
obtain correct results, the 18x20's temperature resolution has
to be adjusted (constant TEMP_RESOLUTION).
* Test configuration:
MCU: PIC18F87J50
Oscillator: HS-PLL 48.0000 MHz, 8.0000 MHz Crystal
Ext. Modules: None.

Par M. JAROU 33 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

SW: mikroC PRO for PIC

* NOTES:
- Place DS18x20 in proper position and place jumper J3 in
G1.7 position.
*/

#include "Resources.h"

/*************************************
TFT module connections
*************************************/

char TFT_DataPort at LATJ;


sbit TFT_RST at LATD3_bit;
sbit TFT_RS at LATE0_bit;
sbit TFT_CS at LATD2_bit;
sbit TFT_RD at LATH1_bit;
sbit TFT_WR at LATH2_bit;
sbit TFT_BLED at LATC2_bit;

char TFT_DataPort_Direction at TRISJ;


sbit TFT_RST_Direction at TRISD3_bit;
sbit TFT_RS_Direction at TRISE0_bit;
sbit TFT_CS_Direction at TRISD2_bit;
sbit TFT_RD_Direction at TRISH1_bit;
sbit TFT_WR_Direction at TRISH2_bit;
sbit TFT_BLED_Direction at TRISC2_bit;

/*************************************
End of TFT module connections
*************************************/

unsigned temperature = 0, old_temperature = 1;


char s_temperature[6];

void DrawMainScr() {
TFT_Fill_Screen(CL_WHITE);
TFT_Set_Pen(CL_Black, 1);
TFT_Line(20, 220, 300, 220);
TFT_Line(20, 46, 300, 46);
TFT_Set_Font(&HandelGothic_BT21x22_Regular, CL_RED,
FO_HORIZONTAL);
TFT_Write_Text("DS1820 TEST", 95, 14);
TFT_Set_Font(&HandelGothic_BT21x22_Regular, CL_BLACK,
FO_HORIZONTAL);
TFT_Write_Text("TEMPERATURE", 85, 85);
TFT_Set_Font(&Verdana12x13_Regular, CL_BLACK,
FO_HORIZONTAL);
TFT_Write_Text("mikroMedia Workstation", 19, 223);
TFT_Write_Text("www.mikroe.com", 200, 223);
TFT_Set_Font(&TFT_defaultFont, CL_BLACK, FO_HORIZONTAL);
}

void Display_Temperature(unsigned temp) {


char cntr = 0;
char s_temp[10];
const unsigned short TEMP_RESOLUTION = 9;
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole;

Par M. JAROU 34 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

unsigned int temp_fraction;

TFT_Set_Brush(1, CL_WHITE, 0, 0, 0, 0);


TFT_Set_Pen(CL_RED, 1);
TFT_Rectangle(115, 115, 200, 145);

// Check if temperature is negative


if (temp & 0x8000) {
s_temp[cntr++] = '-';
temp = ~temp + 1;
}

// Extract temp_whole
temp_whole = temp >> RES_SHIFT ;

// Convert temp_whole to characters


if (temp_whole/100)
s_temp[cntr++] = temp_whole/100 + 48;
// else
// s_temp[cntr++] = '0';

s_temp[cntr++] = (temp_whole/10)%10 + 48;


// Extract tens digit
s_temp[cntr++] = temp_whole%10 + 48;
// Extract ones digit
s_temp[cntr++] = '.';

// Extract temp_fraction and convert it to unsigned int


temp_fraction = temp << (4-RES_SHIFT);
temp_fraction &= 0x000F;
temp_fraction *= 625;

// Convert temp_fraction to characters


s_temp[cntr++] = temp_fraction/1000 + 48;
// Extract thousands digit

s_temp[cntr++] = ' ';


s_temp[cntr++] = ' ';
s_temp[cntr++] = 0xB0;
s_temp[cntr++] = 'C';
s_temp[cntr] = 0;

TFT_Set_Font(&HandelGothic_BT21x22_Regular, CL_BLACK,
FO_HORIZONTAL);
TFT_Write_Text(s_temp, 120, 120);

return;
}

void main() {
WS_Init();
TFT_Init(320,240);
DrawMainScr();

// Main loop
do {
// Perform temperature reading

Par M. JAROU 35 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Ow_Reset(&WS_G1_B7_PORT, WS_G1_B7_PIN);
// Onewire reset signal
Ow_Write(&WS_G1_B7_PORT, WS_G1_B7_PIN, 0xCC);
// Issue command SKIP_ROM
Ow_Write(&WS_G1_B7_PORT, WS_G1_B7_PIN, 0x44);
// Issue command CONVERT_T
Delay_us(120);

Ow_Reset(&WS_G1_B7_PORT, WS_G1_B7_PIN);
Ow_Write(&WS_G1_B7_PORT, WS_G1_B7_PIN, 0xCC);
// Issue command SKIP_ROM
Ow_Write(&WS_G1_B7_PORT, WS_G1_B7_PIN, 0xBE);
// Issue command READ_SCRATCHPAD

temperature = Ow_Read(&WS_G1_B7_PORT, WS_G1_B7_PIN);


temperature = (Ow_Read(&WS_G1_B7_PORT, WS_G1_B7_PIN) << 8)
+ temperature;

// Format and display result on TFT


if(temperature!=old_temperature)
Display_Temperature(temperature);
old_temperature = temperature;

Delay_ms(500);
} while (1);
}

Objectifs
Le but de cette activité est d’apprendre ………. A la fin de cette séance, vous devez
être capable ……………….
Schéma et description
Travail pratique
Programme source

Par M. JAROU 36 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

/*
* Project name:PWM
* Description:
This is a simple demonstration of PWM library. The
modules are initialized and started, after which duty ratios
can be adjusted by potentiometer.
The changes can be monitored LEDs.
* Test configuration:
MCU: PIC18F87J50
Oscillator: HS-PLL 48.0000 MHz, 8.0000 MHz Crystal
Ext. Modules: None
SW: mikroC PRO for PIC

* NOTES:
- Turn on GROUP2 LEDs.
- Place ADC input jumper in A0 position and use
potentiometer P2 to change duty ratio.
*/

unsigned int pwm_period, adc_value, adc_old = 0xFF;


unsigned char current_duty = 0;

void InitMain() {
WS_Init();
ADC_Init();

WS_GROUP2_Set_Output(WS_BIT_MASK_ALL);
WS_GROUP2_Out(0x00);
}

void main() {
InitMain();

PWM2_Init(5000);
// Initiate PWM2
PWM3_Init(3000);
// Initiate PWM3
PWM4_Init(10000);
// Initiate PWM4
PWM5_Init(15000);
// Initiate PWM5

PWM2_Start();
PWM3_Start();
PWM4_Start();
PWM5_Start();

while (1) {
// Endless loop
adc_value = ADC_Get_Sample(WS_A0) >> 2;
if (adc_old != adc_value) {
current_duty = adc_value;
// Duty cycle is between 0 and 255
PWM2_Set_Duty(current_duty);
// set newly acquired duty ratio
PWM3_Set_Duty(255-current_duty);
// set newly acquired duty ratio
PWM4_Set_Duty(current_duty);
// set newly acquired duty ratio

Par M. JAROU 37 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

PWM5_Set_Duty(255-current_duty);
// set newly acquired duty ratio
adc_old = adc_value;
}
Delay_ms(100);
// slow down change pace a little
}
}

Par M. JAROU 38 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Objectifs
Le but de cette activité est d’apprendre ………. A la fin de cette séance, vous devez
être capable ……………….
Schéma et description
Travail pratique
Programme source

/*
* Project name:
UART (Simple usage of UART module library functions)
* Description:
This code demonstrates how to use uart library routines.
Upon receiving data via RS232, MCU immediately sends it back
to the sender.
* Test configuration:
MCU: PIC18F87J50
Oscillator: HS-PLL 48.0000 MHz, 8.0000 MHz Crystal
Ext. Modules: None
SW: mikroC PRO for PIC
* NOTES:

Par M. JAROU 39 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

- Turn on RX and TX switches at SW4 and connect USB cable


*/

char receive;

void main() {
WS_Init();

WS_UART_Init(19200);
Delay_ms(100);
UART_Write_Text("mikroMedia workStation - USB UART Test");
UART_Write(10);
UART_Write(13);
UART_Write_Text("UART Module Initialised");
UART_Write(10);
UART_Write(13);

do {
if (UART_Data_Ready()) {
receive = UART_Read();
UART_Write(receive);
}
} while(1);

Objectifs
Le but de cette activité est d’apprendre ………. A la fin de cette séance, vous devez
être capable ……………….
Schéma et description
Travail pratique
Programme source

Par M. JAROU 40 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Par M. JAROU 41 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

/*
* Project name:
ADC click (Using mikroE's ADC click board)

* Description:
This project is a simple demonstration of working with
the 2.7V 4-Channel
12-bit A/D Converter (MCP3204) with SPI Serial Interface.
The example for measuring on Channels 0, 1, 2 and 3 and
displaying the results on the Lcd.
* Test configuration:
MCU: PIC18F45K22
Dev.Board: EasyPIC7
Oscillator: HS-PLL 32.0000 MHz, 8.0000 MHz Crystal
Ext. Modules: ADC click board - ac:ADCclick

SW: mikroC PRO for PIC

* NOTES:
- Place ADC click board at the mikroBUS socket 1 on the
EasyPIC7 board.
- Put power supply jumper (J5) on the EasyPIC7 board in
3.3V position.
- Maximal SPI clock frequency for MCP3204 running at 3.3V
is 1MHz.
- Input analog value should be in the range of 0 - 3.3V.
*/

// ADC click module connections


sbit Chip_Select_Direction at TRISE0_bit;
sbit Chip_Select at LATE0_bit;
// eof ADC click module connections

// Lcd module connections


sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;


sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End Lcd module connections

unsigned int measurement, lastValue;

// Get ADC values


unsigned int getADC(unsigned short channel) {
// Returns 0..4095
unsigned int tmp;
Chip_Select = 0;
// Select MCP3204
SPI1_Write(0x06);
// SPI communication using 8-bit segments
channel = channel << 6;

Par M. JAROU 42 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

// Bits 7 & 6 define ADC input


tmp = SPI1_Read(channel) & 0x0F;
// Get first 8 bits of ADC value
tmp = tmp << 8;
// Shift ADC value by 8
tmp = tmp | SPI1_Read(0);
// Get remaining 4 bits of ADC value

// and form 12-bit ADC value


Chip_Select= 1;
// Deselect MCP3204
return tmp;
// Returns 12-bit ADC value
}

// Write measured values to Lcd


void processValue(unsigned int pv, unsigned short channel) {
char i, lcdRow, lcdCol;

if (channel < 2)
// If ADC channel 0 or 1 is selected
lcdRow = 1;
// write in the first Lcd row
else
// If ADC channel 2 or is selected
lcdRow = 2;
// write in the second Lcd row

if (channel % 2 > 0 )
// If even ADC channel is selected
lcdCol = 13;
// select Lcd column 13
else
// If odd ADC channel is selected
lcdCol = 4;
// select Lcd column 4

// Converting the measured value into 4 characters


// and writing them to the Lcd at the appropriate place
i = pv / 1000 + 48;
// Get thousandth digit of the ADC result
Lcd_Chr(lcdRow, lcdCol, i);
// Display it on Lcd
pv = pv % 1000;
i = pv / 100 + 48;
// Get hundreth digit of the ADC result
Lcd_Chr(lcdRow, lcdCol+1, i);
// Display it on Lcd
pv = pv % 100;
i = pv / 10 + 48;
// Get tenth digit of the ADC result
Lcd_Chr(lcdRow, lcdCol+2, i);
// Display it on Lcd
pv = pv % 10;
i = pv + 48;
// Get ones digit of the ADC result
Lcd_Chr(lcdRow, lcdCol+3, i);
// Display it on Lcd
}

Par M. JAROU 43 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

void main() {
ANSELB = 0;
// Configure AN pins as digital
ANSELC = 0;
// Configure AN pins as digital
ANSELE = 0;
// Configure AN pins as digital
SLRCON = 0;
// Configure all PORTS at the standard Slew Rate

Lcd_Init();
// Initialize Lcd

Lcd_Cmd(_LCD_CLEAR); // Clear display


Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

measurement = 0;
// Initialize the measurement variable

Chip_Select = 1; // Deselect MCP3204


Chip_Select_Direction = 0;
// Set chip select pin to be output

// Initialize SPI1 module at 500kHz, data sampled at the


middle of interval
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64,
_SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

Lcd_Out(1,1,"C0= C1=");
// Display channel 0 and 1 ID on Lcd
Lcd_Out(2,1,"C2= C3=");
// Display channel 2 and 3 ID on Lcd

while (1) {
measurement = getADC(0);
// Get ADC result from Channel 0
ProcessValue(measurement,0);
// Writes measured value to Lcd
Delay_ms(10); // Wait 10ms
measurement = getADC(1);
// Get ADC result from Channel 1
ProcessValue(measurement,1);
// Writes measured value to Lcd
Delay_ms(10); // Wait 10ms
measurement = getADC(2);
// Get ADC result from Channel 2
ProcessValue(measurement,2);
// Writes measured value to Lcd
Delay_ms(10); // Wait 10ms
measurement = getADC(3);
// Get ADC result from Channel 3
ProcessValue(measurement,3);
// Writes measured value to Lcd
Delay_ms(10); // Wait 10ms
}
}

Par M. JAROU 44 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Objectifs
Le but de cette activité est d’apprendre ………. A la fin de cette séance, vous devez
être capable ……………….
Schéma et description
Travail pratique
Programme source

/*
* Project name: RTC Click board example
* Description:
This project is simple demonstration how to read and
write date and time from PCF8583 RTC (real-time clock).
Date and time are read from the RTC every 1 second and
printed out on USART.
* Test configuration:
MCU: PIC18F87J50
Oscillator: HS-PLL 48.0000 MHz, 8.0000 MHz Crystal

Par M. JAROU 45 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Ext. Modules: RTC Click

* NOTES:
- Place RTC Click board at mikroBUS 2
- Connect UART cable and turn on RX and TX Switches at
SW3
- Place GND button press level and pull up at G0.0
*/

// Module variables
char seconds = 0, minutes = 0, hours = 0, day = 0, month = 0,
year = 0;
// Global date/time variables

// RTC Definitions
#define RTC_ADDR 0xA0 //0xA0 -> 1 = 0x50

/*************************************************************
*************************************
* PCF8583 Functions
**************************************************************
************************************/

/*************************************************************
*************************************
* Read data from RTC PCF8583
* input : addres of RTC register
* output: value of of RTC register
**************************************************************
************************************/
unsigned char RTC_Read(unsigned char addr){
unsigned char value;

WS_I2C_Start(); // Issue start signal


WS_I2C_Wr(RTC_ADDR);
// Address PCF8583, see PCF8583 datasheet-
WS_I2C_Wr(addr); // Start from address 2
WS_I2C_Start();
// Issue repeated start signal
WS_I2C_Wr(RTC_ADDR);
// Address PCF8583 for reading R/W=1

value = WS_I2C_Rd(0); // Read seconds byte


WS_I2C_Stop(); // Issue stop signal

return value;
}

/*************************************************************
*************************************
* Write data from RTC PCF8583
* input : addres of RTC register, value of of RTC register
**************************************************************
************************************/
void RTC_Write(unsigned char addr, unsigned char value){

WS_I2C_Start(); // Issue start signal


WS_I2C_Wr(RTC_ADDR); // Address PCF8530
WS_I2C_Wr(addr); // Start from address

Par M. JAROU 46 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

WS_I2C_Wr(value); // Write value to RTC register


WS_I2C_Stop(); // Issue stop signal

/*************************************************************
*************************************
* Read time from RTC PCF8583
* input : pointer to variables where RTC data will be stored
* output: variables with RTC data
**************************************************************
************************************/
void Read_Time(unsigned char *p_hours, unsigned char
*p_minutes, unsigned char *p_seconds,
unsigned char *p_date, unsigned char *p_month){

WS_I2C_Start(); // Issue start signal


WS_I2C_Wr(RTC_ADDR);
// Address PCF8583, see PCF8583 datasheet
WS_I2C_Wr(2); // Start from address 2
WS_I2C_Restart(); // Issue repeated start
signal
WS_I2C_Wr(RTC_ADDR + 1);
// Address PCF8583 for reading R/W=1

*p_seconds = WS_I2C_Rd(WS_ACK); // Read seconds byte


*p_minutes = WS_I2C_Rd(WS_ACK); // Read minutes byte
*p_hours = WS_I2C_Rd(WS_ACK); // Read hours byte
*p_date = WS_I2C_Rd(WS_ACK); // Read year/day byte
*p_month = WS_I2C_Rd(WS_NO_ACK); // Read weekday/month byte

WS_I2C_Stop(); // Issue stop signal

/*************************************************************
*************************************
* Write time to RTC PCF8583
* input : variables with RTC data
**************************************************************
************************************/
void Write_Time(unsigned char c_hours, unsigned char
c_minutes, unsigned char c_seconds,
unsigned char c_date, unsigned char c_month){

WS_I2C_Start(); // Issue start signal


WS_I2C_Wr(RTC_ADDR); // Address PCF8583, see PCF8583
datasheet
WS_I2C_Wr(0); // Start from address 0
(configuration memory location)
WS_I2C_Wr(0x80); // Write 0x80 to configuration
memory location (pause counter...)

WS_I2C_Wr(0); // Write 0 to cents memory location


WS_I2C_Wr(c_seconds); // Write value to seconds memory
location
WS_I2C_Wr(c_minutes); // Write value to minutes memory
location
WS_I2C_Wr(c_hours); // Write value to hours memory location

Par M. JAROU 47 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

WS_I2C_Wr(c_date); // Write value to year/date


memory location
WS_I2C_Wr(c_month); // Write value to weekday/month
memory location
WS_I2C_Stop(); // Issue stop signal

WS_I2C_Start(); // Issue start signal


WS_I2C_Wr(RTC_ADDR); // Address PCF8530
WS_I2C_Wr(0); // Start from address 0
WS_I2C_Wr(0); // Write 0 to configuration
memory location (enable counting)
WS_I2C_Stop(); // Issue stop signal
}

/*************************************************************
*************************************
* Show on the LCD display
* input : variables with RTC data
**************************************************************
************************************/
void Show_Time(){
char
s_seconds[3],s_minutes[3],s_hours[3],s_year[5],s_day[3],s_mont
h[3];

seconds = ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F);


// Transform seconds
minutes = ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F);
// Transform months
hours = ((hours & 0xF0) >> 4)*10 + (hours & 0x0F);
// Transform hours
year = (day & 0xC0) >> 6;
// Transform year
day = ((day & 0x30) >> 4)*10 + (day & 0x0F);
// Transform day
month = ((month & 0x10) >> 4)*10 + (month & 0x0F);
// Transform month

UART_Write((day / 10) + 48); // Print tens digit of day


variable
UART_Write((day % 10) + 48); // Print oness digit of
day variable
UART_Write('.');
UART_Write((month / 10) + 48);
UART_Write((month % 10) + 48);
UART_Write('.');
UART_Write_Text("201");
UART_Write(year + 48); // Print year variable
(start from year 2010)
UART_Write('.');
UART_Write(10);
UART_Write(13);

UART_Write((hours / 10) + 48);


UART_Write((hours % 10) + 48);
UART_Write(':');
UART_Write((minutes / 10) + 48);
UART_Write((minutes % 10) + 48);
UART_Write(':');
UART_Write((seconds / 10) + 48);

Par M. JAROU 48 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

UART_Write((seconds % 10) + 48);


UART_Write(10);
UART_Write(13);
UART_Write(10);
UART_Write(13);
}

//------------------ Performs project-wide init


void Init_Main() {
WS_Init();

WS_G0_B0_DIR = WS_GPIO_INPUT;

WS_I2C_Init(100000);
WS_UART_Init(19200);
Delay_ms(100);
}

//----------------- Main procedure


void main() {
Init_Main(); // Perform initialization

RTC_Write(0, 0);

while(1){ // Endless loop


if (WS_G0_B0_IN == 0){
while(!WS_G0_B0_IN); // Wait until button is released
// write TIME: 19:16:00 and DATE: 23.11.2011
Write_Time(0x19, 0x16, 0x00, 0x40 | 0x23, 0x11);
}

Read_Time(&hours, &minutes, &seconds, &day, &month);


Show_Time();
Delay_mS(1000);
}
}

Par M. JAROU 49 | P a g e
TP N°1 : Prise en main du Kit Workstation V7

Objectifs
Le but de cette activité est d’apprendre ………. A la fin de cette séance, vous devez
être capable ……………….
Schéma et description
Travail pratique
Programme source

Par M. JAROU 50 | P a g e

Vous aimerez peut-être aussi