Технический форум

Технический форум (http://www.tehnari.ru/)
-   Микропроцессоры (http://www.tehnari.ru/f115/)
-   -   Нужна помощь по программированию на Си МК PIC (http://www.tehnari.ru/f115/t86625/)

Larik 21.03.2013 17:15

Нужна помощь по программированию на Си МК PIC
 
Здравствуйте.
При написании программы на Си для МК PIC столкнулся с проблемой организации задержек.
Стандартный файл в компиляторе HI-TECH Software "delay.h" заточен под 4 МГц, а мне необходимо работать с 20 МГц.
Есть ли у кого макрос или собранная функция задержки
Или что изменить в стандартом файле для корректной работы МК при организации задержек мкс и мс.

Благодарен за помощь в данном вопросе.

korsaj 21.03.2013 18:01

http://diymicro.ru/pic-mk-eksperimen...etodiodom.html

Larik 21.03.2013 21:54

Такие статьи я читал, но я говорил о том, что задержку необходимо организовать на 20Мгц частоты.

korsaj 22.03.2013 10:20

Вложений: 1
ПЛОХО ЧИТАЛ!!!
Носом ткнуть НАДО!!!

korsaj 22.03.2013 10:24

Извините за агрессию...

Larik 22.03.2013 11:02

Вы знаете, всё ведь на так просто.
Полазив в папке с компилятором я нашёл только два фала, которые связаны с задержками, и то, один из них просто демонстрационный.

#include <htc.h> // Required to interface with delay routines

#ifndef _XTAL_FREQ
// Unless already defined assume 4MHz system frequency
// This definition is required to calibrate __delay_us() and __delay_ms()
#define _XTAL_FREQ 20000000
#endif

/** demonstration of various built-in delay mechanisms */
void main(void){
while(1){
NOP();
_delay(1000); // delay for 1000 instruction cycles
CLRWDT();
__delay_us(400); // delay for 400 microseconds
CLRWDT();
__delay_ms(2); // delay for 2 milliseconds
CLRWDT();
}
}




и файл с расширением ___.h

/*
* Delay functions for HI-TECH C on the PIC
*
* Functions available:
* DelayUs(x) Delay specified number of microseconds
* DelayMs(x) Delay specified number of milliseconds
*
* Note that there are range limits: x must not exceed 255 - for xtal
* frequencies > 12MHz the range for DelayUs is even smaller.
* To use DelayUs it is only necessary to include this file; to use
* DelayMs you must include delay.c in your project.
*
*/

/* Set the crystal frequency in the CPP predefined symbols list in
HPDPIC, or on the PICC commmand line, e.g.
picc -DXTAL_FREQ=4MHZ

or
picc -DXTAL_FREQ=100KHZ

Note that this is the crystal frequency, the CPU clock is
divided by 4.

* MAKE SURE this code is compiled with full optimization!!!

*/

#ifndef XTAL_FREQ
#define XTAL_FREQ 4MHZ /* Crystal frequency in MHz */
#endif

#define MHZ *1000L /* number of kHz in a MHz */
#define KHZ *1 /* number of kHz in a kHz */

#if XTAL_FREQ >= 12MHZ

#define DelayUs(x) { unsigned char _dcnt; \
_dcnt = (x)*((XTAL_FREQ)/(12MHZ)); \
while(--_dcnt != 0) \
continue; }
#else

#define DelayUs(x) { unsigned char _dcnt; \
_dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1; \
while(--_dcnt != 0) \
continue; }
#endif

extern void DelayMs(unsigned char);

Larik 22.03.2013 11:04

Только незадача, как подогнать их на мою частоту, чтоб можно было использовать полноценно мс и мкс..

Насчёт команды _delay_ms(n) так вот она не компилится, т.к такая комбинация нигде в подгрузочных файлах не встречается...

korsaj 22.03.2013 12:52

Какая у вас версия компилатора? И выложите сюда ваш "delay.h".

korsaj 22.03.2013 12:56

Цитата:

Сообщение от Larik (Сообщение 884465)
Насчёт команды _delay_ms(n) так вот она не компилится, т.к такая комбинация нигде в подгрузочных файлах не встречается...

А первый пример компилится?
Код:

#include <htc.h> // Required to interface with delay routines

#ifndef _XTAL_FREQ
// Unless already defined assume 4MHz system frequency
// This definition is required to calibrate __delay_us() and __delay_ms()
#define _XTAL_FREQ 20000000
#endif

/** demonstration of various built-in delay mechanisms */
void main(void){
while(1){
NOP();
_delay(1000); // delay for 1000 instruction cycles
CLRWDT();
__delay_us(400); // delay for 400 microseconds
CLRWDT();
__delay_ms(2); // delay for 2 milliseconds
CLRWDT();
}
}


Larik 22.03.2013 17:47

HI-TECH Software 9.7
Единственный файл именуемый delay.h в компиляторе выглядит так вот:

/*
* Delay functions for HI-TECH C on the PIC
*
* Functions available:
* DelayUs(x) Delay specified number of microseconds
* DelayMs(x) Delay specified number of milliseconds
*
* Note that there are range limits: x must not exceed 255 - for xtal
* frequencies > 12MHz the range for DelayUs is even smaller.
* To use DelayUs it is only necessary to include this file; to use
* DelayMs you must include delay.c in your project.
*
*/

/* Set the crystal frequency in the CPP predefined symbols list in
HPDPIC, or on the PICC commmand line, e.g.
picc -DXTAL_FREQ=4MHZ

or
picc -DXTAL_FREQ=100KHZ

Note that this is the crystal frequency, the CPU clock is
divided by 4.

* MAKE SURE this code is compiled with full optimization!!!

*/

#ifndef XTAL_FREQ
#define XTAL_FREQ 4MHZ /* Crystal frequency in MHz */
#endif

#define MHZ *1000L /* number of kHz in a MHz */
#define KHZ *1 /* number of kHz in a kHz */

#if XTAL_FREQ >= 12MHZ

#define DelayUs(x) { unsigned char _dcnt; \
_dcnt = (x)*((XTAL_FREQ)/(12MHZ)); \
while(--_dcnt != 0) \
continue; }
#else

#define DelayUs(x) { unsigned char _dcnt; \
_dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1; \
while(--_dcnt != 0) \
continue; }
#endif

extern void DelayMs(unsigned char);


Часовой пояс GMT +4, время: 13:16.

Powered by vBulletin® Version 4.5.3
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.