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», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

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

Интервал:

Закладка:

Сделать

to a PC through the USB link.

A Visual Basic program runs on the PC where the user enters the bits to be

set or cleared on PORTB of the microcontroller. The PC sends a command to

the microcontroller requesting it to set or reset the required bits of the

microcontroller PORTB.

A 8MHz crystal is used to operate the microcontroller. The actual CPU clock

is raised to 48MHz by setting configuration bits. Also, the USB module is

operated with 48MHz.

The command sent by the PC to the microcontroller is in the following format:

P=nT

where n is the byte the microcontroller is requested to send to PORTB of the

microcontroller.

This program includes the USB code.

Author: Dogan Ibrahim

Date: September 2007

File: USB1.C

****************************************************************************/

#include "C:\Program Files\Mikroelektronika\mikroC\Examples\EasyPic4\extra_examples\HIDlibrary\USBdsc.c"

unsigned char Read_buffer[64];

unsigned char Write_buffer[64];

unsigned char num;

//

// Timer interrupt service routine

//

void interrupt() {

HID_InterruptProc(); // Keep alive

TMR0L = 100; // Re-load TMR0L

INTCON.TMR0IF = 0; // Re-enable TMR0 interrupts

}

//

// Start of MAIN program

//

void main() {

ADCON1 = 0xFF; // Set PORTB to digital I/O

TRISB = 0; // Set PORTB to outputs

PORTB = 0; // Clear all outputs

//

// Set interrupt registers to power-on defaults

// Disable all interrupts

//

INTCON=0;

INTCON2=0xF5;

INTCON3=0xC0;

RCON.IPEN=0;

PIE1=0;

PIE2=0;

PIR1=0;

PIR2=0;

//

// Configure TIMER 0 for 3.3ms interrupts. Set prescaler to 256

// and load TMR0L to 100 so that the time interval for timer

// interrupts at 48MHz is 256*(256-100)*0.083 = 3.3ms

//

// The timer is in 8-bit mode by default

//

T0CON = 0x47; // Prescaler = 256

TMR0L = 100; // Timer count is 256-156 = 100

INTCON.TMR0IE = 1; // Enable T0IE

T0CON.TMR0ON = 1; // Turn Timer 0 ON

INTCON = 0xE0; // Enable interrupts

//

// Enable USB port

//

Hid_Enable(&Read_buffer, &Write_buffer);

Delay_ms(1000);

Delay_ms(1000);

//

// Read from the USB port. Number of bytes read is in num

//

for(;;) // do forever

{

num=0;

while(num != 4) // Get 4 characters

{

num = Hid_Read();

}

if (Read_buffer[0] == 'P' && Read_buffer[1] == '=' &&

Read_buffer[3] == 'T') {

PORTB = Read_buffer[2];

}

}

Hid_Disable();

}

картинка 274

Figure 8.22: Microcontroller program with USB code

Inside the main program PORTB is defined as digital I/O and TRISB is cleared to 0 so all PORTB pins are outputs. All the interrupt registers are then set to their power-on-reset values for safety. The timer interrupts are then set up. The timer is operated in 8-bit mode with a prescaler of 256. Although the crystal clock frequency is 8MHZ, the CPU is operated with a 48MHz clock, as described later. Selecting a timer value of TMR0L . 100 with a 48MHz clock (CPU clock period of 0.083μs) gives timer interrupt intervals of:

(256 – 100) * 256 * 0.083μs

or, about 3.3ms. Thus, the keep-alive messages are sent every 3.3ms.

The USB port is then enabled by calling function Hid_Enable . The program then enters an indefinite loop and reads data from the USB port with Hid_Read . When 4 bytes are received at the correct format (i.e., byte 0=“P,” byte 1=“.”, and byte 3=“T”) then the data byte is read from byte 2 and sent to PORTB of the microcontroller.

It is important to note that when data is received using the Hid_Read function, the function returns the number of bytes received. In addition, the first byte received is the first actual data byte and not the report ID.

Microcontroller Clock

The USB module of the PIC18F4550 microcontroller requires a 48MHz clock. In addition, the microcontroller CPU requires a clock that can range from 0 to 48MHz. In this project the CPU clock is set to be 48MHz.

There are several ways to provide the required clock pulses.

Figure 8.23 shows part of the PIC18F4550 clock circuit. The circuit consists of a 1:1–1:12 PLL prescaler and multiplexer, a 4:96MHz PLL, a 1:2–1:6 PLL postscaler, and a 1:1–1:4 oscillator postscaler. Assuming the crystal frequency is 8MHz and we want to operate the microcontroller with a 48MHz clock, and also remembering that a 48MHz clock is required for the USB module, we should make the following choices in the Edit Project option of the mikroC IDE:

• Set _PLL_DIV2_1L so the 8MHz clock is divided by 2 to produce 4MHZ at the output of the PLL prescaler multiplexer. The output of the 4:96MHZ PLL is now 96MHz. This is further divided by 2 to give 48MHz at the input of multiplexer USBDIV.

• Check _USBDIV_2_1L to provide a 48MHz clock to USB module and to select ÷2 for the PLL postscaler.

• Check CPUDIV_OSC1_PLL2_1L to select PLL as the clock source.

• Check _FOSC_HSPLL_HS_1H to select a 48MHz clock for the CPU.

• Set the CPU clock to 48MHz in mikroC IDE (using Edit Project).

Figure 823 PIC18F4550 microcontroller clock The clock bits selected for the - фото 275

Figure 8.23: PIC18F4550 microcontroller clock

The clock bits selected for the 48MHz USB operation with a 48MHz CPU clock are shown in Figure 8.24.

Figure 824 Selecting clock bits for USB operation Setting other configuration - фото 276

Figure 8.24: Selecting clock bits for USB operation

Setting other configuration bits in addition to the clock bits is recommended.

The following list gives all the bits that should be set in the Edit Project option of the IDE (most of these settings are the power-on-reset values of the bits):

PLLDIV_2_1L

CPUDIV_OSC1_PLL2_1L

USBDIV_2_1L

FOSC_HSPLL_HS_1H

FCMEM_OFF_1H

IESO_OFF_1H

PWRT_ON_2L

BOR_ON_2L

BORV_43_2L

VREGEN_ON_2L

WDT_OFF_2H

WDTPS_256_2H

MCLRE_ON_3H

LPT1OSC_OFF_3H

PBADEN_OFF_3H

CCP2MX_ON_3H

STVREN_ON_4L

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «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