{"action":"created","issue":{"url":"https://api.github.com/repos/buserror/simavr/issues/125","repository_url":"https://api.github.com/repos/buserror/simavr","labels_url":"https://api.github.com/repos/buserror/simavr/issues/125/labels{/name}","comments_url":"https://api.github.com/repos/buserror/simavr/issues/125/comments","events_url":"https://api.github.com/repos/buserror/simavr/issues/125/events","html_url":"https://github.com/buserror/simavr/issues/125","id":97334437,"number":125,"title":"Timer: Broken timing in CTC mode","user":{"login":"frej","id":544679,"avatar_url":"https://avatars.githubusercontent.com/u/544679?v=3","gravatar_id":"","url":"https://api.github.com/users/frej","html_url":"https://github.com/frej","followers_url":"https://api.github.com/users/frej/followers","following_url":"https://api.github.com/users/frej/following{/other_user}","gists_url":"https://api.github.com/users/frej/gists{/gist_id}","starred_url":"https://api.github.com/users/frej/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/frej/subscriptions","organizations_url":"https://api.github.com/users/frej/orgs","repos_url":"https://api.github.com/users/frej/repos","events_url":"https://api.github.com/users/frej/events{/privacy}","received_events_url":"https://api.github.com/users/frej/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2015-07-26T17:07:31Z","updated_at":"2016-08-30T19:21:58Z","closed_at":null,"body":"The current timer implementation fails to correctly handle code which\r\nsets the OCRxA register in (at least) CTC mode.\r\n\r\nThe following program, when compiled for an atmega328 and run on real\r\nhardware, will produce a square wave at ~300Hz with a duty on cycle of\r\n50% on PB3. Running it on simavr gives ~234Hz and a duty cycle of 40%.\r\n\r\n```C\r\n/* Fuses: E:07, H:D9, L:D7 */\r\n#define F_CPU 16000000UL\r\n#include <avr/io.h>\r\n#include <avr/interrupt.h>\r\n#include <util/delay.h>\r\nISR(TIMER2_COMPA_vect)\r\n{\r\n\tstatic int phase = 0;\r\n\tPORTB ^= _BV(3);\r\n\tif (phase)\r\n\t\t_delay_ms(1.0);\r\n\tphase = !phase;\r\n\tOCR2A = 25;\r\n}\r\nint main (void)\r\n{\r\n\tsei(); /* Enable interrupts */\r\n\tDDRB |= _BV(3);\r\n\t/* Timer in CTC mode, interrupt when compare match */\r\n\tOCR2A = 25;\r\n\tTIMSK2 = _BV(OCIE2A);\r\n\tTCCR2A = _BV(WGM21);\r\n\tTCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);\r\n\r\n\twhile (1)\r\n\t\t;\r\n\treturn 0;\r\n}\r\n\r\n```\r\n\r\nLooking at the implementation, it appears that there is a bug in how\r\nthe cycle timer delay is calculated. When the delay to the next\r\ncompare match is calculated the current TCNTx is not considered.\r\nUnfortunately it is not clear to me how this can be fixed without\r\nsubtly breaking the other timer modes.\r\n"},"comment":{"url":"https://api.github.com/repos/buserror/simavr/issues/comments/243550168","html_url":"https://github.com/buserror/simavr/issues/125#issuecomment-243550168","issue_url":"https://api.github.com/repos/buserror/simavr/issues/125","id":243550168,"user":{"login":"bsekisser","id":4514993,"avatar_url":"https://avatars.githubusercontent.com/u/4514993?v=3","gravatar_id":"","url":"https://api.github.com/users/bsekisser","html_url":"https://github.com/bsekisser","followers_url":"https://api.github.com/users/bsekisser/followers","following_url":"https://api.github.com/users/bsekisser/following{/other_user}","gists_url":"https://api.github.com/users/bsekisser/gists{/gist_id}","starred_url":"https://api.github.com/users/bsekisser/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bsekisser/subscriptions","organizations_url":"https://api.github.com/users/bsekisser/orgs","repos_url":"https://api.github.com/users/bsekisser/repos","events_url":"https://api.github.com/users/bsekisser/events{/privacy}","received_events_url":"https://api.github.com/users/bsekisser/received_events","type":"User","site_admin":false},"created_at":"2016-08-30T19:21:58Z","updated_at":"2016-08-30T19:21:58Z","body":"Check out issue #169...  similar if not the same here...  @messani looks to be on to something. "}}
{"actor":{"display_login":"bsekisser"}}