Using OC1A as a 1MHz Clock for a MOS6581

Hello world (Post#1 mandatory catchphrase :slight_smile: ).
Or more accurately Arduino Forum World.

As a newcomer I’m trying to get a MOS6581 working with my brand new Arduino kit, and I would like to use the OC1A pin as a clock for the chip.

Hence, based on a Arduino Uno, I’ve been digging in the forum to find the way to configure accurately the registers to have this pin behave like a 1Mhz clock, assuming a 16Mhz crystal on the Arduino Board.
… and eventually copy/pasted the following code from another guy, and then modified it to my need:

	// === Setup of the CLK for MOS 6581 === 
	// =====================================
	// 1MHz generation on OC1A - Clk 16 MHz

	// set pin 9 as output
	pinMode(9,OUTPUT);
	// reset settings of Timer/Counter register 1
	TCCR1A &= ~((1<<COM1A1) | (1<<COM1A0) | (1<<WGM11) | (1<<WGM10));
	TCCR1B &= ~((1<<WGM13) | (1<<WGM12) | (1<<CS12) | (1<<CS11) | (1<<CS10));

	// set compare match output A to toogle
	TCCR1A |= (0<<COM1A1) | (1<<COM1A0);
	// set waveform generation mode to CTC (Clear Counter on Match)
	TCCR1A |= (0<<WGM11) | (0<<WGM10);
	TCCR1B |= (0<<WGM13) | (1<<WGM12);
	// set clock select to clock/1 (from prescaler)
	TCCR1B |= (0<<CS12) | (0<<CS11) | (1<<CS10);
	// set output compare register A to 8 (i.e. OC1A Toggle every 8 Clk pulses)
	OCR1A = 8;

This is my understanding of the whole thing:

  1. to set the prescaler to divide by 1 ( CS10 = 1)

  2. set output compare register A to 15624
    OCR1A = 15624
    ==> to reset the counter at 8 counts,

  3. To put the output in Toggle mode

As far as I understand it, this would make the OC1A output to toggle every 8 clock pulses at 16MHz, which makes a 1Mhz clock.

Do you agree with this assumption? Am I correct?
I’m mainly wandering if I’m doign the good asumptions from counter point of view, the main issue is that I do not have any scope to check, and I have only ONE former MOS6581 from the first generation, and my worst nightmare would be to destroy the chip…
Thanks in advance for your kind help on this topic.

2) set output compare register A to 15624 OCR1A = 15624 ==> to reset the counter at 8 counts,

Why would you set OCR1A to 15624 to reset the count at 8? Why wouldn't you set OCR1A to 8 to reset the count at 8?

As far as I understand it, this would make the OC1A output to toggle every 8 clock pulses at 16MHz, which makes a 1Mhz clock.

There is a formula in section "15.9.2 Clear Timer on Compare Match (CTC) Mode" of the dataset for calculating the frequency...

fOCnA = fclk_I/O / (2 ? N ? (1 + OCRnA)) fOCnA = 16000000 / (2 ? N ? (1 + OCRnA)) fOCnA = 16000000 / (2 ? 1 ? (1 + 15624)) fOCnA = 512 Hz

A bit off. Let's try working the equation backwards...

fOCnA = fclk_I/O / (2 ? N ? (1 + OCRnA)) (2 ? N ? (1 + OCRnA)) * fOCnA = fclk_I/O (2 ? N ? (1 + OCRnA)) = fclk_I/O / fOCnA (1 + OCRnA) = fclk_I/O / (2 ? N ?fOCnA) OCRnA = (fclk_I/O / (2 ? N ? fOCnA)) - 1 OCRnA = (16000000 / (2 ? 1 ? 1000000)) - 1 OCRnA = 7

There you go. When the processor is running at 16 MHz and the prescaler is 1, OCR1A = 7 gives an output of 1 MHz.

Thank you for the calculation, very helpfull!

My mistake for the 15624, It's a copy paste error. I wanted to write 8, not 15624 which comes from the previous code I have copy/pasted.

I should have done this calculation, I would have catched the - 1 detail.

Regards,

I´ve got an equal proplem, but I need a frequency of 2 mhz. Can you show me, what I must change in your code to get my frequency.

Sorry but this is one of my first program, so I need a little more time to understand the code.

@ref110: Please do not multi-post... http://arduino.cc/forum/index.php/topic,62964.0.html

Thank you Coding Badly (this sounds weird btw) !

You totally nailed it and my mistake was on this asumption, I tested it successfully last night.

And the code follows for those that could help:

// === Setup of the CLK for MOS 6581 === 
// 1MHz generation on OC1A - Clk 16 MHz - set pin 9 as OC1A output
pinMode(MOS_CLK,OUTPUT);
// reset settings of Timer/Counter register 1
TCCR1A &= ~((1<<COM1A1) | (1<<COM1A0) | (1<<WGM11) | (1<<WGM10));
TCCR1B &= ~((1<<WGM13) | (1<<WGM12) | (1<<CS12) | (1<<CS11) | (1<<CS10));
// set compare match output A to toogle
TCCR1A |= (0<<COM1A1) | (1<<COM1A0);
// set waveform generation mode to CTC (Clear Counter on Match)
TCCR1A |= (0<<WGM11) | (0<<WGM10);
TCCR1B |= (0<<WGM13) | (1<<WGM12);
// set clock select to clock/1024 (from prescaler)
TCCR1B |= (0<<CS12) | (0<<CS11) | (1<<CS10);
// set output compare register A to 8 (i.e. OC1A Toggle every 7+1=8 Clk pulses)
OCR1A = 7;

D4p0up: Thank you Coding Badly (this sounds weird btw) !

I'm being to think that misnomer is my name. ]:D

You totally nailed it and my mistake was on this asumption, I tested it successfully last night.

Excellent! Thank you for the follow-up.