Ibrahim Dogan - Advanced PIC Microcontroller Projects in C

Здесь есть возможность читать онлайн «Ibrahim Dogan - Advanced PIC Microcontroller Projects in C» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Город: Burlington, Год выпуска: 2008, ISBN: 2008, Издательство: Elsevier Ltd, Жанр: Программирование, Компьютерное железо, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

Advanced PIC Microcontroller Projects in C: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «Advanced PIC Microcontroller Projects in C»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

• The only project book on the PIC 18 series using the C programming language
• Features 20 complete, tried and test projects
• Includes a CD-ROM of all the programs, hex listings, diagrams, and data sheets

Advanced PIC Microcontroller Projects in C — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «Advanced PIC Microcontroller Projects in C», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Using a Semaphore

The program given in Figure 10.14 is working and displays the measured voltage on the PC screen. This program can be improved slightly by using a semaphore to synchronize the display of the measured voltage with the A/D samples. The modified program (RTOS4.C) is given in Figure 10.16. The operation of the new program is as follows:

• The semaphore variable ( sem ) is set to 1 at the beginning of the program.

• Task Get_voltage decrements the semaphore (calls rtos_wait ) variable so that task To_RS232 is blocked (semaphore variable sem =0) and cannot send data to the PC. When a new A/D sample is ready, the semaphore variable is incremented (calls rtos_signal ) and task To_RS232 can continue. Task To_RS232 then sends the measured voltage to the PC and increments the semaphore variable to indicate that it had access to the data. Task Get_voltage can then get a new sample. This process is repeated forever.

//////////////////////////////////////////////////////////////////////////////

//

// SIMPLE RTOS EXAMPLE - VOLTMETER WITH RS232 OUTPUT

// ---------------------------------------------------------------------------

//

// This is a simple RTOS example. Analog voltage to be measured (between 0V

// and +5V) is connected to analog input AN0 of a PIC18F8520 type

// microcontroller. The microcontroller is operated from a 10MHz crystal. In

// addition, an LED is connected to port in RD7 of the microcontroller.

//

// RS232 serial output of the mirocontroller (RC6) is connected to a MAX232

// type RS232 voltage level converter chip. The output of this chip can be

// connected to the serial input of a PC (e.g., COM1) so that the measured

// voltage can be seen on the PC screen.

//

// The program consists of 3 tasks called "live", "Get_voltage", and

// "To_RS232".

//

// Task "Live" runs every 200ms and it flashes the LED connected to port RD7

// of the microcontroller to indicate that the program is running and is

// ready to measure voltages.

//

// task "Get_voltage" reads analog voltage from port AN0 and then converts the

// voltage into millivolts and stores in a variable called Volts.

//

// Task "To_RS232" gets the measured voltage and then sends to the PC over

// the RS232 serial line. The serial line is configured to operate at 2400

// Baud (higher Baud rates can also be used if desired).

//

// In this modified program, a semaphore is used to synchronize

// the display of the measured value with the A/D samples.

//

// Programmer: Dogan Ibrahim

// Date: September, 2007

// File: RTOS4.C

//

//////////////////////////////////////////////////////////////////////////////

#include <18F8520.h>

#device adc=10

#use delay (clock=10000000)

#use rs232(baud=2400,xmit=PIN_C6,rcv=PIN_C7)

unsigned int16 adc_value;

unsigned int32 Volts;

int8 sem;

//

// Define which timer to use and minor_cycle for RTOS

//

#use rtos(timer=0, minor_cycle=100ms)

//

// Declare TASK "Live" - called every 200ms

// This task flashes the LED on port RD7

//

#task(rate=200ms, max=1ms)

void Live() {

output_toggle(PIN_D7); // Toggle RD7 LED

}

//

// Declare TASK "Get_voltage" - called every 10ms

//

#task(rate=2s, max=100ms)

void Get_voltage() {

rtos_wait(sem); // decrement semaphore

adc_value = read_adc(); // Read A/D value

Volts = (unsigned int32)adc_value*5000;

Volts = Volts / 1024; // Voltage in mV

rtos_signal(sem); // increment semaphore

}

//

// Declare TASK "To_RS232" - called every millisecond

//

#task(rate=2s, max=100ms)

void To_RS232() {

rtos_wait(sem); // Decrement semaphore

printf("Measured Voltage = %LumV\n\r",Volts); // Send to RS232

rtos_signal(sem); // Increment semaphore

}

//

// Start of MAIN program

//

void main() {

set_tris_d(0); // PORTD all outputs

output_d(0); // Clear PORTD

set_tris_a(0xFF); // PORTA all inputs

setup_adc_ports(ALL_ANALOG); // A/D ports

setup_adc(ADC_CLOCK_DIV_32); // A/D clock

set_adc_channel(0); // Select channel 0 (AN0)

delay_us(10);

sem = 1; // Semaphore is 1

rtos_run(); // Start RTOS

}

картинка 331

Figure 10.16: Modified program listing

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «Advanced PIC Microcontroller Projects in C»

Представляем Вашему вниманию похожие книги на «Advanced PIC Microcontroller Projects in C» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «Advanced PIC Microcontroller Projects in C»

Обсуждение, отзывы о книге «Advanced PIC Microcontroller Projects in C» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x