Haberler:

Foruma Resim Yükleme ve Boyut Sınırlaması ( ! )  https://bit.ly/2GMFb8H

Ana Menü

12f1822 frekans sorunu

Başlatan madiac, 20 Şubat 2013, 19:37:32

madiac

Merhaba,

Yeni projelerde kullanmak için 12f1822yi denemek istedim, kendisinin maşallahı var. Ama garip bir sorunla karşılaştım: frekans ayarladığım değerlerde çalışmıyor.
Internal 16 MHz kullanmak istiyorum, başlangıç olarak yazılımla pwm oluşturmak istedim üç ledi farklı pwmlerle çalıştırmak için. Hesapladığımdan farklı bir periyotla çalışıyor pwm. Hesabımda mı sıkıntı var yoksa programda mı bilemedim.

PWMi yazılımla oluşturmak için timer 0 kullandım. Ayrıca elimde 12f1822 olmadığı için proteusta denedim.

Hesabı da şu şekilde yaptım: 16 MHz clock, yani 0.25 us instruction clock; 1:64 prescaler, 16 us period timer 0; 8 bit timer 0, 128 us pwm period. 4 ms period alıyorum ama.

Belki pwm oluştururken sorun yaşamışımdır dedim, clock out özelliğini açıp frekansına bakayım dedim onu da beceremedim, clock out hiç alamıyorum.

#include <htc.h>
#include "Settings.h"

/* Config register CONFIG1 settings */
__CONFIG( FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF &
        BOREN_OFF & CLKOUTEN_ON & IESO_OFF & FCMEN_ON );
/************************************/

/* Config register CONFIG2 settings */
__CONFIG( WRT_OFF & PLLEN_OFF & STVREN_OFF & LVP_ON );
/************************************/

unsigned char PWM[3] = { 0, 0, 0 };	// PWM values for R, G, and B

void main()
{
	InternalClockSettings();
	GeneralInterruptSettings();
	IOSettings();
	Timer0Settings();
	
	PWM[0] = 0x80;
	PWM[1] = 0x40;
	PWM[2] = 0x20;
	
	while(1)
	{
		if( TMR0 < PWM[0] )	// R output
			RA2 = 1;
		else
			RA2 = 0;
		
		if( TMR0 < PWM[1] )	// G output
			RA4 = 1;
		else
			RA4 = 0;
		
		if( TMR0 < PWM[2] )	// B output
			RA5 = 1;
		else
			RA5 = 0;
		
	} // end of while
} // end of function main


Bu da ayarları yaptığım settings.c

#include <htc.h>
 
 void InternalClockSettings()
{
	IRCF0 = 1;	// 16 MHz internal clock
	IRCF1 = 1;
	IRCF2 = 1;
	IRCF3 = 1;
	
	SCS0 = 1;
	SCS1 = 1;

        SPLLEN = 0;     // No 4x PLL
} // end of function InternalClockSettings

void GeneralInterruptSettings()
{
	PEIE = 1;	// Peripheral interrupts are enabled
	GIE = 1;	// General interrupts are enabled
} // end of function GeneralInterruptSettings

void IOSettings()
{
	ANSELA = 0x00;	// All digital I/O
	TRISA = 0x0A;	// RA1 & RA2 are input, others output
	PORTA = 0x00;	
} // end of function IOSettings

void Timer0Settings()
{
	TMR0CS = 0;	// Timer mode is enabled
	
	PS0 = 1;	// Prescaler is 1:64
	PS1 = 0;
	PS2 = 1;
	
	PSA = 0;
} // end of function Timer0Settings


Kodla bu kadar debelleştikten sonra yanlış hesaplamışsın derseniz üzülürüm :D

Yardımcı olursanız pek sevinirim.

iyildirim

Öncelikle Foruma Hoşgeldin.

Geçenlerde basit bir iş için aynı aileden 12f1840 kullanmak zorunda kaldım. 
Sadece Sigorta ayarlayarak 16Mhz çalışmam diye diretti. Errata' ya göre osilator ile ilgili bir sorun var ve 16Mhz için clock switching yapmak ve PLL yi sonradan aktif etmek gerekiyor.
Ama Clockout da hiç bir sorun yaşamadım. Kodunu incelemedim ama örnek kod ile clockout da 8Mhz görmen lazım. 


Config bits..
// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF       // Power-up Timer Enable (PWRT enabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = ON   // Clock Out Enable 
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF       // PLL Enable (4x PLL enabled)
#pragma config STVREN = OFF     // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
#pragma config BORV = HI        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

Osilatör ..
void ConfigureOscillator(void)
{
    _nop();
    _nop();
    OSCCONbits.SCS      = 0b00;
    _nop();
    _nop();
    _nop();
    _nop();
    OSCCONbits.IRCF     = 0b1110;

unsigned char  t = 255;
while (t>=1)
{
    t--;
    _nop();
    _nop();
    _nop();
    _nop();
    _nop();
    _nop();
    _nop();
    _nop();
    _nop();
    _nop();
    _nop();
    _nop();
    _nop();
    _nop();
    _nop();
    _nop();
    _nop();
}
    _nop();
    _nop();
    while(OSCSTATbits.HFIOFR==0);
    _nop();
    _nop();
    while(OSCSTATbits.HFIOFL==0);
    _nop();
    _nop();
    while(OSCSTATbits.HFIOFS==0);
    _nop();
    _nop();
    OSCCONbits.SPLLEN   = 0b1;
    _nop();
    while(OSCSTATbits.PLLR==0);
    _nop();
    _nop();
    _nop();
    _nop();
}