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

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

Интервал:

Закладка:

Сделать

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

SIMPLE DICE

===========

In this project 7 LEDs are connected to PORTC of a PIC18F452 microcontroller

and the microcontroller is operated from a 4MHz resonator. The LEDs are

organized as the faces of a real dice. When a push-button switch connected

to RB0 is pressed a dice pattern is displayed on the LEDs. The display

remains in this state for 3 seconds and after this period the LEDs all turn

OFF to indicate that the system is ready for the button to be pressed again.

In this program a pseudorandom number generator function is

used to generate the dice numbers between 1 and 6.

Author: Dogan Ibrahim

Date: July 2007

File: LED3.C

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

#define Switch PORTB.F0

#define Pressed 0

//

// This function generates a pseudo random integer number

// between 1 and Lim

//

unsigned char Number(int Lim, int Y) {

unsigned char Result;

static unsigned int Y;

Y = (Y * 32719 + 3) % 32749;

Result = ((Y % Lim) + 1);

return Result;

}

//

// Start of MAIN program

//

void main() {

unsigned char J,Pattern,Seed = 1;

unsigned char DICE[] = {0,0x08,0x22,0x2A,0x55,0x5D,0x77};

TRISC = 0; // PORTC outputs

TRISB = 1; // RB0 input

PORTC = 0; // Turn OFF all LEDs

for(;;) // Endless loop

{

if(Switch == Pressed) // Is switch pressed ?

{

J = Number(6,seed); // Generate a number between 1 and 6

Pattern = DICE[J]; // Get LED pattern

PORTC = Pattern; // Turn on LEDs

Delay_ms(3000); // Delay 3 second

PORTC = 0; // Turn OFF all LEDs

}

}

}

картинка 182

Figure 6.11: Dice program using a pseudorandom number generator

The operation of the program is basically same as in Figure 6.10. When the push-button switch is pressed, function Number is called to generate a new dice number between 1 and 6, and this number is used as an index in array DICE in order to find the bit pattern to be sent to the LEDs.

PROJECT 6.3 — Two-Dice Project

Project Description

This project is similar to Project 2, but here a pair of dice are used — as in many dice games such as backgammon — instead of a single dice.

The circuit shown in Figure 6.8 can be modified by adding another set of seven LEDs for the second dice. For example, the first set of LEDs can be driven from PORTC, the second set from PORTD, and the push-button switch can be connected to RB0 as before. Such a design requires fourteen output ports just for the LEDs. Later on we will see how the LEDs can be combined in order to reduce the input/output requirements. Figure 6.12 shows the block diagram of the project.

Figure 612 Block diagram of the project Project Hardware The circuit - фото 183

Figure 6.12: Block diagram of the project

Project Hardware

The circuit diagram of the project is shown in Figure 6.13. The circuit is basically same as in Figure 6.8, with the addition of another set of LEDs connected to PORTD.

Figure 613 Circuit diagram of the project Project PDL The operation of the - фото 184

Figure 6.13: Circuit diagram of the project

Project PDL

The operation of the project is very similar to that for Project 2. Figure 6.14 shows the PDL for this project. At the beginning of the program the PORTC and PORTD pins are configured as outputs, and bit 0 of PORTB (RB0) is configured as input. The program then executes in a loop continuously and checks the state of the push-button switch. When the switch is pressed, two pseudorandom numbers between 1 and 6 are generated, and these numbers are sent to PORTC and PORTD. The LEDs remain at this state for 3 seconds, after which all the LEDs are turned OFF to indicate that the push-button switch can be pressed again for the next pair of numbers.

START

Create DICE table

Configure PORTC as outputs

Configure PORTD as outputs

Configure RB0 as input

DO FOREVER

IF button pressed THEN

Get a random number between 1 and 6

Find bit pattern

Turn ON LEDs on PORTC

Get second random number between 1 and 6

Find bit pattern

Turn on LEDs on PORTD

Wait 3 seconds

Turn OFF all LEDs

ENDIF

ENDDO

END

картинка 185

Figure 6.14: PDL of the project

Project Program

The program is called LED4.C, and the program listing is given in Figure 6.15. At the beginning of the program Switch is defined as bit 0 of PORTB, and Pressed is defined as 0. The relationships between the dice numbers and the LEDs to be turned on are stored in an array called DICE , as in Project 2. Variable Pattern is the data sent to the LEDs. Program enters an endless for loop where the state of the push-button switch is checked continuously. When the switch is pressed, two random numbers are generated by calling function Number . The bit patterns to be sent to the LEDs are then determined and sent to PORTC and PORTD. The program then repeats inside the endless loop, checking the state of the push-button switch.

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

TWO DICE

========

In this project 7 LEDs are connected to PORTC of a PIC18F452 microcontroller

and 7 LEDs to PORTD. The microcontroller is operated from a 4MHz resonator.

The LEDs are organized as the faces of a real dice. When a push-button switch

connected to RB0 is pressed a dice pattern is displayed on the LEDs. The

display remains in this state for 3 seconds and after this period the LEDs

all turn OFF to indicate that the system is ready for the button to be pressed

again.

In this program a pseudorandom number generator function is

used to generate the dice numbers between 1 and 6.

Author: Dogan Ibrahim

Date: July 2007

File: LED4.C

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

#define Switch PORTB.F0

#define Pressed 0

//

// This function generates a pseudo random integer number

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

Интервал:

Закладка:

Сделать

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